首页 > 解决方案 > 下一个反应构建失败并出现错误错误:“/app/node_modules/i18next-http-middleware”的包导出未定义有效的“./cjs”目标

问题描述

当我运行 build server.js 时发生错误,我添加了负责此代码的文件,请让我知道我需要添加我的代码的更多详细信息

应用 # npm 运行服务器:开发

合作伙伴门户@0.1.0 服务器:dev /app 节点 .build/server

内部/模块/cjs/loader.js:548 抛出 e;^

Error: Package exports for '/app/node_modules/i18next-http-middleware' do not define a valid './cjs' target
    at resolveExportsTarget (internal/modules/cjs/loader.js:545:13)
    at applyExports (internal/modules/cjs/loader.js:459:14)
    at resolveExports (internal/modules/cjs/loader.js:508:12)
    at Function.Module._findPath (internal/modules/cjs/loader.js:577:20)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:879:27)
    at Function.Module._load (internal/modules/cjs/loader.js:785:27)
    at Module.require (internal/modules/cjs/loader.js:956:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/app/node_modules/next-i18next/dist/commonjs/middlewares/next-i18next-middleware.js:28:35)
    at Module._compile (internal/modules/cjs/loader.js:1063:30) {
  code: 'MODULE_NOT_FOUND'
}

我的 package.json 文件

{
    "name": "partner-portal",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@reduxjs/toolkit": "^1.2.3",
        "@sentry/browser": "^5.15.0",
        "@sentry/integrations": "^5.15.1",
        "@sentry/node": "^5.15.0",
        "@zeit/next-less": "^1.0.1",
        "@zeit/next-sass": "^1.0.1",
        "@zeit/next-source-maps": "0.0.3",
        "antd": "^3.26.7",
        "axios": "^0.19.2",
        "axios-observable": "^1.1.3",
        "bootstrap": "^4.4.1",
        "cookie-parser": "^1.4.5",
        "dotenv": "^8.2.0",
        "dotenv-extended": "^2.7.1",
        "express": "^4.16.4",
        "file-loader": "^5.0.2",
        "i18next": "^14.0.1",
        "js-cookie": "^2.2.1",
        "less": "^3.11.1",
        "less-vars-to-js": "^1.3.0",
        "momentjs": "^2.0.0",
        "next": "^9.2.2",
        "next-compose-plugins": "^2.2.0",
        "next-cookies": "^2.0.3",
        "next-i18next": "^4.2.0",
        "next-redux-saga": "^4.1.2",
        "next-redux-wrapper": "^4.0.1",
        "node-sass": "^4.13.1",
        "null-loader": "^3.0.0",
        "prettier": "^1.19.1",
        "react": "^16.12.0",
        "react-alice-carousel": "^1.17.3",
        "react-dom": "^16.12.0",
        "react-html-parser": "^2.0.2",
        "react-lottie": "^1.2.3",
        "react-player": "^1.15.2",
        "react-redux": "^7.1.3",
        "react-router-dom": "^5.1.2",
        "react-scripts": "^3.3.0",
        "react-slick": "^0.25.2",
        "react-spring": "^8.0.27",
        "redux": "^4.0.5",
        "redux-mock-store": "^1.5.4",
        "redux-saga": "^1.1.3",
        "rxjs": "^6.5.4",
        "slick-carousel": "^1.8.1",
        "styled-components": "^5.0.0",
        "swr": "^0.1.17",
        "webpack": "^4.41.6",
        "webpack-filter-warnings-plugin": "^1.2.1",
        "@babel/runtime-corejs2": "^7.9.6"
    },
    "scripts": {
        "svgr": "svgr -d src/assets/icons/svg/dist src/assets/icons/svg/ --no-view-box false",
        "dev": "npm-run-all clean transpile server:dev",
        "start": "npm-run-all clean transpile build",
        "build": "NODE_OPTIONS=--max-old-space-size=${NODE_MAX_MEM} next build",
        "server:prod": "NODE_ENV=production node .build/server",
        "server:dev": "node .build/server",
        "transpile": "babel server.ts i18n.ts sentry.ts --extensions \".ts\" --out-dir .build",
        "clean": "rimraf dist-server",
        "test:coverage": "yarn test --watchAll false --coverage",
        "lint": "tsc --noEmit && eslint '*/**/*.{ts,tsx}'",
        "lint:fix": "yarn lint --quiet --fix",
        "transpile:test": "npx webpack ",
        "server:test": "node .build/server",
        "dev:test": "npm-run-all clean transpile:test server:test",
        "babel-preset-react": "^6.24.1"
    },
    "eslintConfig": {
        "extends": "react-app"
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    },
    "devDependencies": {
        "@svgr/cli": "^5.1.0",
        "@svgr/webpack": "^5.2.0",
        "@testing-library/dom": "^6.11.0",
        "@testing-library/jest-dom": "^5.0.2",
        "@testing-library/react": "^9.4.0",
        "@testing-library/user-event": "^8.0.5",
        "@types/express": "^4.17.2",
        "@types/jest": "^24.9.1",
        "@types/next": "^9.0.0",
        "@types/node": "^13.5.0",
        "@types/react": "^16.9.19",
        "@types/react-dom": "^16.9.5",
        "@types/react-redux": "^7.1.7",
        "@types/react-router-dom": "^5.1.3",
        "@types/redux-mock-store": "^1.0.2",
        "@types/redux-saga": "^0.10.5",
        "@types/styled-components": "^4.4.2",
        "@typescript-eslint/eslint-plugin": "^2.18.0",
        "@typescript-eslint/parser": "^2.18.0",
        "eslint": "^6.8.0",
        "eslint-config-prettier": "^6.10.0",
        "eslint-plugin-prettier": "^3.1.2",
        "eslint-plugin-react": "^7.18.0",
        "lodash": "^4.17.15",
        "typescript": "^3.7.5",
        "rimraf": "^3.0.2",
        "npm-run-all": "^4.1.5",
        "@babel/cli": "^7.8.4",
        "babel-loader": "^8.0.6",
        "babel-plugin-import": "^1.13.0",
        "babel-plugin-inline-import": "^3.0.0",
        "babel-plugin-inline-react-svg": "^1.1.1",
        "babel-plugin-lodash": "^3.3.4",
        "babel-plugin-react-require": "^3.1.3",
        "babel-plugin-styled-components": "^1.10.7",
        "babel-preset-react": "^6.24.1"
    }
}

服务器.ts

import express from "express";
import next from "next";
import uuidv4 from "uuid/v4";
import cookieParser from "cookie-parser";
import nextI18NextMiddleware from "next-i18next/middleware";
import nextI18next from "./i18n";
import sentry from "./sentry";
import * as dot_env from "dotenv";

dot_env.config();
const port = process.env.PORT || 3002;
const app = next({ dev: process.env.NODE_ENV !== "production" });
const appHandler = app.getRequestHandler();
const dev = process.env.NODE_ENV !== "production";

function sessionCookie(req, res, next) {
    const htmlPage =
        !req.path.match(/^\/(_next|static)/) &&
        !req.path.match(/\.(js|map)$/) &&
        req.accepts("text/html", "text/css", "image/png") === "text/html";

    if (!htmlPage) {
        next();
        return;
    }

    if (!req.cookies.sid || req.cookies.sid.length === 0) {
        req.cookies.sid = uuidv4();
        res.cookie("sid", req.cookies.sid);
    }

    next();
}

const sourcemapsForSentryOnly = token => (req, res, next) => {
    // In production we only want to serve source maps for Sentry
    if (!dev && !!token && req.headers["x-sentry-token"] !== token) {
        res.status(401).send(
            "Authentication access token is required to access the source map."
        );
        return;
    }
    next();
};

(async () => {
    const { Sentry } = sentry(app.buildId);
    await app.prepare();
    await nextI18next.initPromise;
    console.log(`App will be listening in port : ${port}`);
    await express()
        .use(nextI18NextMiddleware(nextI18next))
        .use(cookieParser())
        .use(sessionCookie)
        .get(/\.map$/, sourcemapsForSentryOnly(process.env.SENTRY_TOKEN))
        .use(Sentry.Handlers.requestHandler())
        .get("*", (req, res) => appHandler(req, res))
        .listen(port);
})();

和 i18n.ts

import NextI18Next from "next-i18next";
export const Lang = {
    default_lang: "en",
    other_lang: ["bn"],
    local_sub_path: {
        en: "en",
        bn: "bn"
    },
    default_namespace: "common",
    lang_folder: "public/lang"
};

const NextI18NextInstance = new NextI18Next({
    defaultLanguage: Lang.default_lang,
    defaultNS: Lang.default_namespace,
    localePath: Lang.lang_folder,
    otherLanguages: Lang.other_lang,
    localeSubpaths: Lang.local_sub_path,
    fallbackNS: Lang.default_namespace,
    interpolation: {
        format: (val, format, lang) => {
            if (val instanceof Date) {
                return "";
            }
            return val;
        }
    }
});

export const {
    appWithTranslation,
    useTranslation,
    Trans,
    Link,
    Router,
    withTranslation
} = NextI18NextInstance;

export default NextI18NextInstance;

这是我在 docker env 中的项目根目录中的节点包列表的输出


/app # npm list|grep next
+-- @zeit/next-less@1.0.1npm
| +-- @zeit/next-css@1.0.1 ERR! peer dep missing: less@^2.7.3, required by @zeit/next-less@1.0.1
npm ERR! peer dep missing: jquery@1.9.1 - 3, required by bootstrap@4.4.1
npm ERR! peer dep missing: jquery@>=1.8.0, required by slick-carousel@1.8.1
npm ERR! peer dep missing: popper.js@^1.16.0, required by bootstrap@4.4.1

+-- @zeit/next-sass@1.0.1
| +-- @zeit/next-css@1.0.1 deduped
+-- @zeit/next-source-maps@0.0.3
+-- @zeit/next-typescript@1.1.1
+-- i18next-http-middleware@1.0.6
+-- next@9.3.6
| +-- @next/react-refresh-utils@9.3.6
| | | | `-- next-tick@1.0.0
+-- next-compose-plugins@2.2.0
+-- next-cookies@2.0.3
+-- next-i18next@4.4.1
| +-- i18next@19.4.4
| +-- i18next-browser-languagedetector@4.1.1
| +-- i18next-fs-backend@1.0.3
| +-- i18next-http-backend@1.0.10
| +-- i18next-http-middleware@1.0.6 deduped
| +-- react-i18next@11.4.0 deduped
+-- next-redux-saga@4.1.2
+-- next-redux-wrapper@4.0.1
| |   +-- process-nextick-args@2.0.1
+-- react-i18next@11.4.0

标签: node.jsreactjsnext.jsnext-i18next

解决方案


当我升级到 Big Sur 时,我的全局(系统)节点包似乎以与上述错误堆栈跟踪类似的方式中断。

使用nvm并安装 v14(系统默认为 13),我能够运行我的netlifycli 工具而没有上述问题。

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
nvm install v14
# Output something like: "Now using node v14.17.0 (npm v6.14.13)"
npm install -g netlify-cli

(这将可执行文件放入/Users/{you}/.nvm/versions/node/v14.17.0/bin/netlify


推荐阅读