React项目VPS部署执行npm build报错,本地VS Code运行正常求助
React应用部署(Ubuntu+Nginx)npm build报错解决
问题描述
我尝试在Ubuntu系统的VPS上部署React应用(搭配Nginx),但执行npm build时持续报错,本地VS Code运行完全正常,错误信息如下:
> mystery@0.1.0 build /home/react-projects/portfolio > react-scripts build /home/react-projects/portfolio/node_modules/eslint-webpack-plugin/node_modules/jest-worker/build/index.js:110 _ending; ^ 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 Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at Object.<anonymous> (/home/react-projects/portfolio/node_modules/eslint-webpack-plugin/dist/getESLint.js:9:5) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! mystery@0.1.0 build: `react-scripts build` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the mystery@0.1.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! /root/.npm/_logs/2022-08-23T18_10_04_282Z-debug.log
解决方案
这个错误根源是VPS上的Node.js版本过低,不支持依赖包jest-worker中使用的新JavaScript语法。按以下步骤修复:
检查当前Node.js版本
执行命令查看版本:node -vReact官方要求Node.js版本至少14.0.0,若你的版本低于此,必须升级。
升级Node.js到兼容版本
推荐通过官方PPA安装稳定版本(以18.x为例):# 安装必要依赖 sudo apt update && sudo apt install -y ca-certificates curl gnupg # 添加Node.js官方GPG密钥 curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/nodesource.gpg # 添加Node.js 18.x源 echo "deb [signed-by=/etc/apt/trusted.gpg.d/nodesource.gpg] https://deb.nodesource.com/node_18.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list # 安装Node.js sudo apt update && sudo apt install -y nodejs重新安装依赖并执行构建
清理旧依赖和缓存后重新安装:rm -rf node_modules package-lock.json npm install npm run build
若升级Node后仍有问题,可尝试降低react-scripts版本到与当前Node兼容的范围,但优先升级Node是最彻底的解决方式。
内容的提问来源于stack exchange,提问作者Piet
相关产品推荐
相关产品推荐

