温馨提示×

debian nodejs如何调试

小樊
54
2026-09-01 16:54:33
栏目: 编程语言

在 Debian 上调试 Node.js 程序,常见方式有以下几种,按使用频率和场景整理:


一、最常用:命令行调试(node --inspect)

1. 启动调试

node --inspect app.js

或监听断点(代码开始前暂停):

node --inspect-brk app.js

输出示例:

Debugger listening on ws://127.0.0.1:9229/xxxx

2. 用浏览器调试

  • 打开 Chrome
  • 访问:chrome://inspect
  • 点击 Open dedicated DevTools for Node

即可像前端一样打断点、看变量。


二、使用 VS Code 调试(推荐)

1. 安装

Debian 上安装 VS Code:

sudo apt install code

2. 配置 launch.json

在项目中:

.vscode/launch.json

示例:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Node",
      "program": "${workspaceFolder}/app.js"
    }
  ]
}

直接按 F5 即可调试。


三、命令行 REPL / 日志调试

1. console 调试

console.log(obj)
console.error(err)

2. 交互式运行

node

或:

node -e "console.log(1+1)"

四、调试已有进程(生产环境)

1. 查看 node 进程

ps aux | grep node

2. 给运行中的进程开启调试

kill -USR1 <pid>

然后:

node --inspect

五、常见问题(Debian 特有)

1. node 命令不存在

Debian 默认可能是旧版本:

node -v

推荐用 NodeSource:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt install -y nodejs

2. 权限问题

不要使用 root 跑 node:

sudo useradd -m nodeuser
sudo su nodeuser

六、进阶工具

  • ndb(增强版调试器)
npx ndb app.js
  • node --prof(性能分析)
  • clinic.js(性能诊断)

如果你有具体场景(如:Express、PM2、Docker、生产环境),我可以给你更精确的调试方案。

0 踩