NestJS部署至cPanel失败求助:启动命令报错如何解决?
cPanel部署NestJS后端启动报错解决方案
核心错误信息
> test3@0.0.1 start /home/nandaken/inventory > nest start stderr: npm WARN lifecycle The node binary used for scripts is /home/nandaken/nodevenv/inventory/12/bin/node but npm is using /opt/alt/alt-nodejs12/root/usr/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with. sh: nest: command not found npm ERR! code ELIFECYCLE npm ERR! syscall spawn npm ERR! file sh npm ERR! errno ENOENT npm ERR! test3@0.0.1 start: `nest start` npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the test3@0.0.1 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
问题原因
- cPanel环境未全局安装Nest CLI,且本地安装的Nest CLI不在系统PATH中,直接执行
nest start会找不到命令 - Node.js二进制文件路径不匹配,npm脚本使用的node路径与当前npm环境的node路径不一致
解决步骤
1. 修改启动脚本,调用本地Nest CLI
直接使用项目本地安装的Nest CLI,无需依赖全局环境:
- 打开项目根目录的
package.json,修改scripts里的start命令:
或者用"scripts": { "start": "node node_modules/@nestjs/cli/bin/nest.js start" }npx自动调用本地依赖:"scripts": { "start": "npx nest start" }
2. 修复Node路径不一致问题
任选一种方式即可:
- 执行启动命令时添加参数:
npm run start --scripts-prepend-node-path - 或者在项目根目录创建
.npmrc文件,添加以下内容实现永久生效:scripts-prepend-node-path=true
3. 确保本地已安装Nest CLI
检查package.json的devDependencies中是否包含@nestjs/cli,如果没有,执行以下命令安装:
npm install @nestjs/cli --save-dev
内容的提问来源于stack exchange,提问作者wahyu nanda
相关产品推荐
相关产品推荐

