在 CentOS 上调试 Node.js,常见有几种方式,下面按使用场景给你总结一下。
--inspectnode --inspect app.js
默认监听:
ws://127.0.0.1:9229
终端会输出类似:
Debugger listening on ws://127.0.0.1:9229/xxxx
node --inspect=9229 app.js
node --inspect-brk app.js
⚠️ 程序会在第一行暂停,方便打断点
CentOS 通常在服务器上,本地是 Windows / Mac
node --inspect=0.0.0.0:9229 app.js
⚠️ 安全警告:
在本地执行:
ssh -L 9229:127.0.0.1:9229 user@centos_ip
CentOS 上运行:
node --inspect app.js
本地浏览器打开:
chrome://inspect
node --inspect=9229 app.js
launch.json{
"type": "node",
"request": "attach",
"name": "Attach to CentOS",
"address": "127.0.0.1",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/path/to/project"
}
pm2 start app.js --node-args="--inspect=9229"
pm2 list
node app.js 2>&1 | tee log.txt
或:
pm2 logs
firewall-cmd --list-ports
firewall-cmd --add-port=9229/tcp --permanent
firewall-cmd --reload
sudo node --inspect app.js
| 场景 | 命令 |
|---|---|
| 本地调试 | node --inspect app.js |
| 首行断点 | node --inspect-brk app.js |
| 远程 | --inspect=0.0.0.0:9229 |
| 安全 | SSH 隧道 |
| 生产 | PM2 + inspect |
如果你用的是 CentOS 7 / 8 / Stream,或者 Docker / PM2 / NestJS / Express,可以告诉我具体环境,我可以给你更精确的方案。