在 Debian 上调试 Node.js 程序,常见方式有以下几种,按使用频率和场景整理:
node --inspect app.js
或监听断点(代码开始前暂停):
node --inspect-brk app.js
输出示例:
Debugger listening on ws://127.0.0.1:9229/xxxx
chrome://inspect即可像前端一样打断点、看变量。
Debian 上安装 VS Code:
sudo apt install code
在项目中:
.vscode/launch.json
示例:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Node",
"program": "${workspaceFolder}/app.js"
}
]
}
直接按 F5 即可调试。
console.log(obj)
console.error(err)
node
或:
node -e "console.log(1+1)"
ps aux | grep node
kill -USR1 <pid>
然后:
node --inspect
Debian 默认可能是旧版本:
node -v
推荐用 NodeSource:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt install -y nodejs
不要使用 root 跑 node:
sudo useradd -m nodeuser
sudo su nodeuser
ndb(增强版调试器)npx ndb app.js
node --prof(性能分析)clinic.js(性能诊断)如果你有具体场景(如:Express、PM2、Docker、生产环境),我可以给你更精确的调试方案。