在 CentOS 上调试 Node.js 应用,常见方式有 命令行调试、VS Code 远程调试、日志调试 等。下面按「从简单到专业」给你一套实用配置方案。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 18
nvm use 18
node -v
node inspectnode inspect app.js
常用命令:
c 继续执行n 单步s 进入函数o 跳出repl 查看变量适合:简单脚本、线上快速排查
node --inspect=0.0.0.0:9229 app.js
打开 Chrome:
chrome://inspect
服务器IP:9229⚠️ 注意防火墙:
firewall-cmd --add-port=9229/tcp --permanent
firewall-cmd --reload
node --inspect=0.0.0.0:9229 app.js
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to CentOS",
"address": "服务器IP",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/project/path"
}
]
}
✅ 支持:
npm i -g pm2
pm2 start app.js --node-args="--inspect=0.0.0.0:9229"
查看:
pm2 logs
pm2 list
const debug = require('debug')('app');
debug('start server');
启动:
DEBUG=app node app.js
127.0.0.1| 场景 | 方案 |
|---|---|
| 本地开发 | VS Code |
| 测试服务器 | --inspect |
| 生产环境 | 日志 + PM2 |
| 紧急排查 | node inspect |
如果你愿意,可以告诉我:
我可以给你完全定制化的调试配置。