Ubuntu环境下npm run 'build staging'执行失败求助
问题排查与解决方案
可能原因及对应解决步骤
1. Shell解析空格的差异
Ubuntu默认shell可能为dash,而正常运行的系统使用bash,两者对带空格的脚本名解析逻辑存在区别:
- 临时切换shell测试:执行
bash -c "npm run 'build staging'",若能正常运行则说明是shell导致的问题。 - 永久修改默认shell:执行
chsh -s /bin/bash,重启终端后再运行原命令。
2. npm版本不一致
系统版本相同但npm版本不同,也可能引发脚本解析异常:
- 查看当前npm版本:执行
npm -v,对比正常系统的版本号。 - 同步版本:若版本差异较大,执行
npm install -g npm@<正常系统的版本号>,之后重新运行命令。
3. 环境变量TMPDIR异常
错误提示中的/tmp/build指向临时目录,若TMPDIR被篡改,会导致shell错误解析命令:
- 临时重置环境变量:执行
TMPDIR=/tmp npm run 'build staging'进行测试。 - 检查永久配置:查看
~/.bashrc或~/.profile中是否有修改TMPDIR的语句,如有则注释掉并重启终端。
4. npm shell配置错误
npm默认使用的shell被修改,也会造成脚本执行失败:
- 查看当前npm shell配置:执行
npm config get shell。 - 设置为bash:执行
npm config set shell /bin/bash,之后重新运行命令。
长期稳妥解决方案
避免使用带空格的脚本名,改用冒号分隔的格式彻底规避空格解析问题:
修改package.json的scripts段为:
"scripts": { "start:development": "REACT_APP_ENV=development react-scripts start", "start:staging": "REACT_APP_ENV=staging react-scripts start", "start:production": "REACT_APP_ENV=production react-scripts start", "build:development": "REACT_APP_ENV=development react-scripts build", "build:staging": "REACT_APP_ENV=staging react-scripts build", "build:production": "REACT_APP_ENV=production react-scripts build", "build": "react-scripts build", "start": "react-scripts start", "test": "react-scripts test", "eject": "react-scripts eject" }
之后执行npm run build:staging即可正常构建预发布版本。
内容的提问来源于stack exchange,提问作者pjsagar
相关产品推荐
相关产品推荐

