英文:
react app build fails with 'ERR_REQUIRE_ESM'
问题
当我尝试运行我的React应用程序的构建命令时,我看到以下错误导致构建失败:
var stripAnsi = require('strip-ansi');
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\rahul\Documents\project_name\node_modules\strip-ansi\index.js from C:\Users\rahul\Documents\cambian\cambian-widget-client\node_modules\react-dev-utils\FileSizeReporter.js not supported.
Instead change the require of index.js in C:\Users\rahul\Documents\project_name\node_modules\react-dev-utils\FileSizeReporter.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\rahul\Documents\project_name\node_modules\react-dev-utils\FileSizeReporter.js:15:17)
at Object.<anonymous> (C:\Users\rahul\Documents\project_name\node_modules\react-scripts\scripts\build.js:35:26) {
code: 'ERR_REQUIRE_ESM'
}
error Command failed with exit code 1.
但是,当我删除锁定文件(lockfile)并重新运行构建命令时,它会成功执行。然后,直到锁定文件被删除,新的构建再次失败。
这导致了一个重大问题,因为每次进行更改后,我们都必须手动部署应用程序。
这在两周前还能正常工作,我可以确认没有添加新的包,也没有更新任何包。
我尝试过什么?
- 升级了Node、npm、yarn,但都没有起作用
- 运行了
yarn cache clean
- 运行了
yarn audit fix --force
使用的构建命令:
react-scripts build
环境:
node: 16.19.0
npm: 9.8.1
yarn: 1.22.19
React包版本:
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "^5.0.0"
英文:
When I am trying to run the build command for my react application I am seeing this error failing the build:
var stripAnsi = require('strip-ansi');
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\rahul\Documents\project_name\node_modules\strip-ansi\index.js from C:\Users\rahul\Documents\cambian\cambian-widget-client\node_modules\react-dev-utils\FileSizeReporter.js not supported.
Instead change the require of index.js in C:\Users\rahul\Documents\project_name\node_modules\react-dev-utils\FileSizeReporter.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\rahul\Documents\project_name\node_modules\react-dev-utils\FileSizeReporter.js:15:17)
at Object.<anonymous> (C:\Users\rahul\Documents\project_name\node_modules\react-scripts\scripts\build.js:35:26) {
code: 'ERR_REQUIRE_ESM'
}
error Command failed with exit code 1.
But when I delete the lockfile and re-run the build command, it executes successfully. Then again the new builds fails until the lockfile is removed.
This is causing a major issue as we have to manually deploy the application every time a change has been done.
This has been working fine two weeks ago and I can confirm neither new packages have been added nor any package has been updated
What I have tried?
- upgrading node, npm, yarn but nothing worked
- yarn cache clean
- yarn audit fix --force
Build command used:
react-scripts build
Enviroment:
node: 16.19.0
npm: 9.8.1
yarn: 1.22.19
React packages version
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "^5.0.0",
答案1
得分: 1
"唯一解决方法我找到的是在 package.json 中将 strip-ansi
强制降级到 6.0.0 或 6.0.1。在 7.0.0 中它变成了纯 ESM。类似这样:
{
...
"resolutions": {
"strip-ansi": "6.0.0"
}
}
如果你使用 npm
而不是 yarn
(虽然我看到楼主不是这样做的,但对于其他人来说),那么可以使用一个覆盖项:
{
...
"overrides": {
"strip-ansi": "6.0.0"
}
}"
英文:
The only way I have found around this is a resolution in package.json forcing strip-ansi
down to 6.0.0 or 6.0.1. It became pure ESM in 7.0.0. Something like this:
{
...
"resolutions": {
"strip-ansi": "6.0.0"
}
}
If you use npm
rather than yarn
(which I see the OP isn't, but for others), then use an override:
{
...
"overrides": {
"strip-ansi": "6.0.0"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论