React NPM Run构建 - 必须使用导入加载ES模块错误

发布于 2025-02-05 16:03:51 字数 1165 浏览 4 评论 0原文

在尝试为简单的React应用程序运行Docker Build命令时(今天通过我学习React构建),它未能编译,并且错误的第一行显示了以下内容:必须使用导入加载ES模块。以下是完整的错误。

此后,我在node_modules/@eslint/eslintrc/package.json.json文件中做了3件事:

  1. 删除了“ type”:“ model” 代码和测试,
  2. 在第23行上重命名unidence.js unidence.cjs,然后重新添加了在步骤1中删除的代码,
  3. 删除&重命名。

这是两个更改的文件:

错误持续存在,并且错误日志没有差异。我还尝试使用NVM 16,但没有运气。

是什么导致了这个问题,我可以尝试什么?

更新1:package.json的屏幕截图文件:

更新2:项目目录的屏幕截图:

While trying to run a docker build command for a simple React application (built today via create-react-app as I'm learning React), it failed to compile, and the first line of the errors displayed the following: Must use import to load ES Module. Below is the complete error.

command line error

After this, I did 3 things in the node_modules/@eslint/eslintrc/package.json file:

  1. Removed the "type": "model" code and tested,
  2. Renamed on line 23 universal.js to universal.cjs and re-added the code removed in step 1,
  3. Removed & renamed both.

Here is the file with both changes:
enter image description here

The error persisted, and there was no difference in the error log. I also tried to use nvm 16 but had no luck.

What is causing this problem, and what is something I can try?

Update 1: screenshot of package.json file:
enter image description here

Update 2: screenshot of projects directory:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耳钉梦 2025-02-12 16:03:52

在进行了进一步的调试之后,原因似乎是由于React与Dockerfile上使用的节点版本之间的不匹配(React V18和React-Script V5,试图在节点V12上运行)。

要解决,需要更新Dockerfile以基于最新的节点版本,例如:

FROM node:18-alpine

..

After some further debugging, it appears the cause was due to a mismatch between react and the node version used on Dockerfile (react v18 and react-scripts v5, trying to run on node v12).

To solve, it was required to update the Dockerfile to be based off a more recent node version, e.g.:

FROM node:18-alpine

..
过潦 2025-02-12 16:03:52

@nuxurious,自上一周以来,我遇到了这样的问题。
在我的本地环境上,NPM运行和构建都在工作,因为我正在使用节点版本13或更高版本。
在构建的服务器端,编译的过程发生了故障。我们正在使用节点12.16.1。将节点设置为12.16.1之后,我能够在本地上复制相同的问题。希望更新节点将帮助您解决此问题。

// package.json.eslintrc

{
  "name": "dbe-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/eslint-parser": "^7.18.2",
    "@fortawesome/fontawesome-svg-core": "^1.2.36",
    "@fortawesome/free-regular-svg-icons": "^6.1.1",
    "@fortawesome/free-solid-svg-icons": "^6.1.1",
    "@fortawesome/react-fontawesome": "^0.2.0",
    "@progress/kendo-data-query": "^1.6.0",
    "@progress/kendo-drawing": "^1.16.3",
    "@progress/kendo-licensing": "^1.2.2",
    "@progress/kendo-react-animation": "^5.4.0",
    "@progress/kendo-react-buttons": "^5.4.0",
    "@progress/kendo-react-common": "^5.4.0",
    "@progress/kendo-react-data-tools": "^5.4.0",
    "@progress/kendo-react-dateinputs": "^5.4.0",
    "@progress/kendo-react-dialogs": "^5.4.0",
    "@progress/kendo-react-dropdowns": "^5.4.0",
    "@progress/kendo-react-form": "^5.4.0",
    "@progress/kendo-react-grid": "^5.4.0",
    "@progress/kendo-react-inputs": "^5.4.0",
    "@progress/kendo-react-intl": "^5.4.0",
    "@progress/kendo-react-labels": "^5.4.0",
    "@progress/kendo-react-layout": "^5.4.0",
    "@progress/kendo-react-listbox": "^5.4.0",
    "@progress/kendo-react-notification": "^5.4.0",
    "@progress/kendo-react-popup": "^5.4.0",
    "@progress/kendo-react-progressbars": "^5.4.0",
    "@progress/kendo-react-ripple": "^5.4.0",
    "@progress/kendo-react-tooltip": "^5.4.0",
    "@progress/kendo-react-treeview": "^5.4.0",
    "@progress/kendo-theme-default": "^5.5.0",
    "@telerik/kendo-intl": "^2.3.1",
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "axios": "^0.27.2",
    "bootstrap": "^4.6.1",
    "lodash": "^4.17.20",
    "mobx": "^6.6.0",
    "mobx-react": "^7.5.0",
    "node-gyp-build": "^4.2.3",
    "node-polyfill-webpack-plugin": "^2.0.0",
    "path-browserify": "^1.0.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-idle-timer": "^4.6.4",
    "react-router-dom": "^5.3.3",
    "react-scripts": "^5.0.1",
    "reactstrap": "^8.10.1",
    "sass": "^1.53.0",
    "styled-components": "^5.2.1",
    "uuid": "^8.3.2",
    "web-vitals": "^2.1.4",
    "websocket": "^1.0.32"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "overrides": {
    "autoprefixer": "10.4.5"
  },
  "devDependencies": {
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-jsx-a11y": "^6.6.0",
    "eslint-plugin-prettier": "^4.2.1",
    "eslint-plugin-react": "^7.30.1",
    "eslint-plugin-react-hooks": "^4.6.0",
    "prettier": "^2.7.1"
  }
}

//

{
  "env": {
    "browser": true, // Browser global variables like `window` etc.
    "commonjs": true, // CommonJS global variables and CommonJS scoping.Allows require, exports and module.
    "es6": true, // Enable all ECMAScript 6 features except for modules.
    "jest": true, // Jest global variables like `it` etc.
    "node": true // Defines things like process.env when generating through node
  },
  "extends": [
    "react-app",
    "react-app/jest",
    // "airbnb",
    // "prettier",
    // "eslint:recommended",
    "plugin:react/recommended",
    // "plugin:react-hooks/recommended"
  ],
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest" // Allows for the parsing of modern ECMAScript features
  },
  "plugins": [
    // "import",
    // "prettier",
    // "react-hooks"
  ],
  // "root": true, // For configuration cascading.
  "rules": {
    "semi": "warn",
    "comma-dangle": "off",
    "quotes": "off",
    // "arrow-parens": "on",
    // "arrow-body-style": "off",
    "no-console": "off",
    "no-unused-vars": "off",
    "no-param-reassign": "off",
    "operator-linebreak": "off",
    "object-curly-newline": "off",
    "no-unused-expressions": "off",
    // "lines-between-class-members": "off",
    "class-methods-use-this": "off",
    "object-shorthand": "off",
    "import/prefer-default-export": "off",
    "new-cap": "off",
    "prefer-object-spread": "off",
    "prefer-template": "off",
    "prefer-const": "off",
    "linebreak-style": "off",
    "global-require": "off",
    "jsx-a11y/label-has-associated-control": "off",
    "react/destructuring-assignment": "off",
    "react/jsx-fragments": "off",
    "react/jsx-props-no-spreading": "off",
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [
          ".js",
          ".jsx"
        ]
      }
    ],
    "react/prop-types": "off",
    "react/no-access-state-in-setstate": "off",
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn", // Checks effect dependencies
    // kendo ui
    "react/jsx-boolean-value": "off",
    // "prettier/prettier": [
    //   "warn",
    //   {
    //     "semi": true
    //   }
    // ]
  }
}

jsconfig

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "experimentalDecorators": true
  },
  "exclude": ["node_modules"]
}

@Nuxurious, I have had same issue like this since last 2 week.
On my local environment both npm run and build were working find as i was using node version 13 or higher.
While on server side on build compile was getting failed. We were using node 12.16.1. I was able to replicate same issue on my local after setting node to 12.16.1. Hope updating node will help you solve this problem.

// Package.json

{
  "name": "dbe-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/eslint-parser": "^7.18.2",
    "@fortawesome/fontawesome-svg-core": "^1.2.36",
    "@fortawesome/free-regular-svg-icons": "^6.1.1",
    "@fortawesome/free-solid-svg-icons": "^6.1.1",
    "@fortawesome/react-fontawesome": "^0.2.0",
    "@progress/kendo-data-query": "^1.6.0",
    "@progress/kendo-drawing": "^1.16.3",
    "@progress/kendo-licensing": "^1.2.2",
    "@progress/kendo-react-animation": "^5.4.0",
    "@progress/kendo-react-buttons": "^5.4.0",
    "@progress/kendo-react-common": "^5.4.0",
    "@progress/kendo-react-data-tools": "^5.4.0",
    "@progress/kendo-react-dateinputs": "^5.4.0",
    "@progress/kendo-react-dialogs": "^5.4.0",
    "@progress/kendo-react-dropdowns": "^5.4.0",
    "@progress/kendo-react-form": "^5.4.0",
    "@progress/kendo-react-grid": "^5.4.0",
    "@progress/kendo-react-inputs": "^5.4.0",
    "@progress/kendo-react-intl": "^5.4.0",
    "@progress/kendo-react-labels": "^5.4.0",
    "@progress/kendo-react-layout": "^5.4.0",
    "@progress/kendo-react-listbox": "^5.4.0",
    "@progress/kendo-react-notification": "^5.4.0",
    "@progress/kendo-react-popup": "^5.4.0",
    "@progress/kendo-react-progressbars": "^5.4.0",
    "@progress/kendo-react-ripple": "^5.4.0",
    "@progress/kendo-react-tooltip": "^5.4.0",
    "@progress/kendo-react-treeview": "^5.4.0",
    "@progress/kendo-theme-default": "^5.5.0",
    "@telerik/kendo-intl": "^2.3.1",
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "axios": "^0.27.2",
    "bootstrap": "^4.6.1",
    "lodash": "^4.17.20",
    "mobx": "^6.6.0",
    "mobx-react": "^7.5.0",
    "node-gyp-build": "^4.2.3",
    "node-polyfill-webpack-plugin": "^2.0.0",
    "path-browserify": "^1.0.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-idle-timer": "^4.6.4",
    "react-router-dom": "^5.3.3",
    "react-scripts": "^5.0.1",
    "reactstrap": "^8.10.1",
    "sass": "^1.53.0",
    "styled-components": "^5.2.1",
    "uuid": "^8.3.2",
    "web-vitals": "^2.1.4",
    "websocket": "^1.0.32"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "overrides": {
    "autoprefixer": "10.4.5"
  },
  "devDependencies": {
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-jsx-a11y": "^6.6.0",
    "eslint-plugin-prettier": "^4.2.1",
    "eslint-plugin-react": "^7.30.1",
    "eslint-plugin-react-hooks": "^4.6.0",
    "prettier": "^2.7.1"
  }
}

.ESLINTRC

{
  "env": {
    "browser": true, // Browser global variables like `window` etc.
    "commonjs": true, // CommonJS global variables and CommonJS scoping.Allows require, exports and module.
    "es6": true, // Enable all ECMAScript 6 features except for modules.
    "jest": true, // Jest global variables like `it` etc.
    "node": true // Defines things like process.env when generating through node
  },
  "extends": [
    "react-app",
    "react-app/jest",
    // "airbnb",
    // "prettier",
    // "eslint:recommended",
    "plugin:react/recommended",
    // "plugin:react-hooks/recommended"
  ],
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest" // Allows for the parsing of modern ECMAScript features
  },
  "plugins": [
    // "import",
    // "prettier",
    // "react-hooks"
  ],
  // "root": true, // For configuration cascading.
  "rules": {
    "semi": "warn",
    "comma-dangle": "off",
    "quotes": "off",
    // "arrow-parens": "on",
    // "arrow-body-style": "off",
    "no-console": "off",
    "no-unused-vars": "off",
    "no-param-reassign": "off",
    "operator-linebreak": "off",
    "object-curly-newline": "off",
    "no-unused-expressions": "off",
    // "lines-between-class-members": "off",
    "class-methods-use-this": "off",
    "object-shorthand": "off",
    "import/prefer-default-export": "off",
    "new-cap": "off",
    "prefer-object-spread": "off",
    "prefer-template": "off",
    "prefer-const": "off",
    "linebreak-style": "off",
    "global-require": "off",
    "jsx-a11y/label-has-associated-control": "off",
    "react/destructuring-assignment": "off",
    "react/jsx-fragments": "off",
    "react/jsx-props-no-spreading": "off",
    "react/jsx-filename-extension": [
      1,
      {
        "extensions": [
          ".js",
          ".jsx"
        ]
      }
    ],
    "react/prop-types": "off",
    "react/no-access-state-in-setstate": "off",
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn", // Checks effect dependencies
    // kendo ui
    "react/jsx-boolean-value": "off",
    // "prettier/prettier": [
    //   "warn",
    //   {
    //     "semi": true
    //   }
    // ]
  }
}

//JSCONFIG

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "experimentalDecorators": true
  },
  "exclude": ["node_modules"]
}
水溶 2025-02-12 16:03:52

从14/07/2022开始的解决方案:问题是Docker文件中的错误模块版本。在发布此问题后不久就解决了它。

以下 this 可以找到代码在这里,我在Dockerfile中使用了以下行:
来自节点:12.16.3-alpine作为构建

此版本是错误的原因,并使用较新版本解决了:
来自节点:18-alpine作为构建

The solution from 14/07/2022: the issue was the wrong module version in the Docker file. It was solved shortly after posting this question.

Following this tutorial, of which the code can be found here, I used the following line in the Dockerfile:
FROM node:12.16.3-alpine as build

This version was the cause of the error, and was solved using a newer version:
FROM node:18-alpine as build

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文