如何修复构建React App时出现的Syntax Error及ELIFECYCLE错误
npm run build 执行失败,返回 ELIFECYCLE errno 1
每次执行npm run build时,程序中途退出并返回ELIFECYCLE错误码errno 1。这是评审他人代码时遇到的问题,原作者也无法解决。提示存在语法错误,但检查node_modules未发现问题,已通过npm install安装所有依赖,本地也已安装wails、golang、npm、node、vite,问题仍未解决。
执行npm run build时的错误信息:
/home/user_name/path/path/code_folder/frontend/node_modules/vite/bin/vite.js:2 import { performance } from 'node:perf_hooks' ^ SyntaxError: Unexpected token { at Module._compile (internal/modules/cjs/loader.js:723:23) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! frontend@0.0.0 build: `vite build` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the frontend@0.0.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /home/user_name/.npm/_logs/2023-03-29T12_36_29_654Z-debug.log
日志文件内容:
0 info it worked if it ends with ok 1 verbose cli [ '/usr/bin/node', '/bin/npm', 'run', 'build' ] 2 info using npm@6.14.4 3 info using node@v10.19.0 4 verbose run-script [ 'prebuild', 'build', 'postbuild' ] 5 info lifecycle frontend@0.0.0~prebuild: frontend@0.0.0 6 info lifecycle frontend@0.0.0~build: frontend@0.0.0 7 verbose lifecycle frontend@0.0.0~build: unsafe-perm in lifecycle true 8 verbose lifecycle frontend@0.0.0~build: PATH: /usr/share/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/user_name/path/path/project_folder/frontend/node_modules/.bin:/sbin:/bin:/usr/bin:/usr/local/bin:/snap/bin:/usr/local/go/bin 9 verbose lifecycle frontend@0.0.0~build: CWD: /home/user_name/path/path/project_folder/frontend 10 silly lifecycle frontend@0.0.0~build: Args: [ '-c', 'vite build' ] 11 silly lifecycle frontend@0.0.0~build: Returned: code: 1 signal: null 12 info lifecycle frontend@0.0.0~build: Failed to exec build script 13 verbose stack Error: frontend@0.0.0 build: `vite build` 13 verbose stack Exit status 1 13 verbose stack at EventEmitter.<anonymous> (/usr/share/npm/node_modules/npm-lifecycle/index.js:332:16) 13 verbose stack at EventEmitter.emit (events.js:198:13) 13 verbose stack at ChildProcess.<anonymous> (/usr/share/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14) 13 verbose stack at ChildProcess.emit (events.js:198:13) 13 verbose stack at maybeClose (internal/child_process.js:982:16) 13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5) 14 verbose pkgid frontend@0.0.0 15 verbose cwd /home/user_name/path/path/project_folder/frontend 16 verbose Linux 5.15.0-1035-azure 17 verbose argv "/usr/bin/node" "/bin/npm" "run" "build" 18 verbose node v10.19.0 19 verbose npm v6.14.4 20 error code ELIFECYCLE 21 error errno 1 22 error frontend@0.0.0 build: `vite build` 22 error Exit status 1 23 error Failed at the frontend@0.0.0 build script. 23 error This is probably not a problem with npm. There is likely additional logging output above. 24 verbose exit [ 1, true ]
解决方案
核心问题:Node.js版本过低
从日志可见当前使用的是Node.js v10.19.0,而Vite 3.x及以上版本要求Node.js版本至少为v14.18+、v16+。Node.js v10不支持ES模块的import语法(以及node:perf_hooks这种内置模块的新引入方式),导致Vite的启动脚本无法执行。解决步骤:
- 升级Node.js版本:安装v14.18.0或更高版本(推荐LTS版本,比如v16.x或v18.x)。可以使用nvm(Node版本管理器)来管理多个Node版本:
- 安装nvm后,执行
nvm install 16,然后nvm use 16切换版本。
- 安装nvm后,执行
- 清理并重新安装依赖:
- 删除
node_modules文件夹:rm -rf node_modules - 删除
package-lock.json:rm package-lock.json - 重新安装依赖:
npm install
- 删除
- 重新执行构建:
npm run build
- 升级Node.js版本:安装v14.18.0或更高版本(推荐LTS版本,比如v16.x或v18.x)。可以使用nvm(Node版本管理器)来管理多个Node版本:
内容的提问来源于stack exchange,提问作者fr0st
相关产品推荐
相关产品推荐

