使用PM2部署React Build时出现无效PID错误的解决方法求助
解决PM2部署React静态站点时的"TypeError: One of the pids provided is invalid"错误
问题场景
使用serve build 80 --spa可正常运行React项目构建产物,站点能成功访问;但通过pm2 serve build 80 --spa或sudo pm2 serve build 80 --spa用PM2部署时,PM2日志反复出现TypeError: One of the pids provided is invalid错误,站点无法访问。相关命令执行及日志信息如下:
ubuntu@ip-172-31-42-194:~/workspace/frontend$ pm2 serve build 80 --spa [PM2] Starting /usr/local/lib/node_modules/pm2/lib/API/Serve.js in fork_mode (1 instance) [PM2] Done. [PM2] Serving /home/ubuntu/workspace/frontend/build on port 80 ┌────┬──────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐ │ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │ ├────┼──────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┴──────────┴──────────┴──────────┴──────────┤ │ 0 │ static-page-server-80 │ default │ 5.3.0 │ fork │ N/A │ 0s │ 0 │ online │ 0% │ 0b │ ubuntu │ disabled │ └────┴──────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴──────────────────────────────────────────────────────────┘ [PM2][WARN] Current process list is not synchronized with saved list. App static-page-server-8080 server differs. Type 'pm2 save' to synchronize. ubuntu@ip-172-31-42-194:~/workspace/frontend$ pm2 logs [TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option) /home/ubuntu/.pm2/pm2.log last 15 lines: PM2 | 2023-09-01T07:55:23: PM2 error: TypeError: One of the pids provided is invalid PM2 | 2023-09-01T07:55:54: PM2 error: Error caught while calling pidusage PM2 | 2023-09-01T07:55:54: PM2 error: TypeError: One of the pids provided is invalid PM2 | 2023-09-01T07:56:24: PM2 error: Error caught while calling pidusage PM2 | 2023-09-01T07:56:24: PM2 error: TypeError: One of the pids provided is invalid PM2 | 2023-09-01T07:56:54: PM2 error: Error caught while calling pidusage PM2 | 2023-09-01T07:56:54: PM2 error: TypeError: One of the pids provided is invalid PM2 | 2023-09-01T07:57:24: PM2 error: Error caught while calling pidusage PM2 | 2023-09-01T07:57:24: PM2 error: TypeError: One of the pids provided is invalid PM2 | 2023-09-01T07:57:54: PM2 error: Error caught while calling pidusage PM2 | 2023-09-01T07:57:54: PM2 error: TypeError: One of the pids provided is invalid PM2 | 2023-09-01T07:58:24: PM2 error: Error caught while calling pidusage PM2 | 2023-09-01T07:58:24: PM2 error: TypeError: One of the pids provided is invalid PM2 | 2023-09-01T07:58:34: PM2 error: Error caught while calling pidusage PM2 | 2023-09-01T07:58:34: PM2 error: TypeError: One of the pids provided is invalid /home/ubuntu/.pm2/logs/static-page-server-80-out.log last 15 lines: /home/ubuntu/.pm2/logs/static-page-server-80-error.log last 15 lines:
解决方法
1. 清理PM2缓存与异常进程
PM2进程缓存可能存在异常,执行以下步骤彻底重置:
- 终止所有PM2进程:
pm2 kill - 删除PM2缓存目录:
rm -rf ~/.pm2 - 重新初始化PM2:
pm2 init - 重新部署站点:
pm2 serve build 80 --spa
2. 处理低端口权限问题
普通用户无法直接绑定1024以下端口(如80),sudo运行可能导致进程权限异常:
- 先使用高端口测试:
pm2 serve build 8080 --spa,确认站点能正常访问后,再考虑端口映射 - 若必须使用80端口,可给node进程授权绑定低端口:
授权完成后,用普通用户执行sudo setcap 'cap_net_bind_service=+ep' $(which node)pm2 serve build 80 --spa
3. 使用PM2配置文件部署
配置文件比命令行参数更稳定,创建ecosystem.config.js:
module.exports = { apps: [{ name: 'react-static-site', script: 'npx serve', args: 'build --port 80 --spa', interpreter: 'none', watch: false }] };
启动命令:pm2 start ecosystem.config.js
4. 升级PM2版本
部分旧版PM2存在兼容性问题,升级到最新版:
npm install -g pm2@latest
升级后重新尝试部署
5. 检查build目录权限
确保PM2运行用户对build目录有读写权限:
sudo chown -R ubuntu:ubuntu ~/workspace/frontend/build sudo chmod -R 755 ~/workspace/frontend/build
内容的提问来源于stack exchange,提问作者Dark_Shadow
相关产品推荐
相关产品推荐

