在 Debian 上调试 Node.js 有多种方式,取决于你是本地调试、远程调试、还是生产环境排查。下面按常见场景给你一个实用指南。
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
检查:
node -v
npm -v
node inspect app.js
然后在浏览器打开:
chrome://inspect
或使用 CLI:
node inspect app.js
debug> help
node --inspect=0.0.0.0:9229 app.js
launch.json{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Remote",
"address": "服务器IP",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/path/to/project"
}
]
}
⚠️ 生产环境不要暴露 9229 到公网
console.log(process.pid)
process.on('uncaughtException', e => {
console.error(e)
})
debug 模块DEBUG=* node app.js
node --prof app.js
node --prof-process isolate-*.log > profile.txt
node --inspect app.js
Chrome → Memory → Heap Snapshot
ps aux | grep node
ss -tulnp | grep node
sudo strace -p <pid>
lsof -p <pid>
npm i -g pm2
pm2 start app.js --inspect
pm2 logs
pm2 monit
| 问题 | 解决 |
|---|---|
| 端口被占用 | lsof -i:9229 |
| 无法连接 | 防火墙 / 0.0.0.0 |
| 卡死 | strace / heapdump |
| 中文乱码 | LANG=en_US.UTF-8 |
如果你能告诉我:
我可以给你更精确的调试方案。