React NPM Run构建 - 必须使用导入加载ES模块错误
在尝试为简单的React应用程序运行Docker Build命令时(今天通过我学习React构建),它未能编译,并且错误的第一行显示了以下内容:必须使用导入加载ES模块
。以下是完整的错误。
此后,我在node_modules/@eslint/eslintrc/package.json.json
文件中做了3件事:
- 删除了
“ type”:“ model” 代码和测试,
- 在第23行上重命名
unidence.js
unidence.cjs
,然后重新添加了在步骤1中删除的代码, - 删除&重命名。
错误持续存在,并且错误日志没有差异。我还尝试使用NVM 16,但没有运气。
是什么导致了这个问题,我可以尝试什么?
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.
After this, I did 3 things in the node_modules/@eslint/eslintrc/package.json
file:
- Removed the
"type": "model"
code and tested, - Renamed on line 23
universal.js
touniversal.cjs
and re-added the code removed in step 1, - Removed & renamed both.
Here is the file with both changes:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在进行了进一步的调试之后,原因似乎是由于React与Dockerfile上使用的节点版本之间的不匹配(React V18和React-Script V5,试图在节点V12上运行)。
要解决,需要更新Dockerfile以基于最新的节点版本,例如:
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.:
@nuxurious,自上一周以来,我遇到了这样的问题。
在我的本地环境上,NPM运行和构建都在工作,因为我正在使用节点版本13或更高版本。
在构建的服务器端,编译的过程发生了故障。我们正在使用节点12.16.1。将节点设置为12.16.1之后,我能够在本地上复制相同的问题。希望更新节点将帮助您解决此问题。
// package.json.eslintrc
//
jsconfig
@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
.ESLINTRC
//JSCONFIG
从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