在Debian上调试Node.js应用程序有多种方法,以下是一些常用的调试技巧:
debug
包进行调试debug
包:npm install debug
debug
模块:const debug = require('debug')('myapp');
debug('Hello, debug!');
DEBUG
环境变量来控制调试器的输出:DEBUG=myapp node app.js
const debug = require('debug');
const serverDebug = debug.extend('myapp:server');
const databaseDebug = debug.extend('myapp:database');
serverDebug('Hello, server!');
databaseDebug('Hello, database!');
DEBUG_COLORS=true DEBUG_FD=3 node app.js
launch.json
文件:
在 .vscode
目录下创建 launch.json
文件,配置调试任务:{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Node.js",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/app.js"
}
]
}
F5
或点击左侧调试图标,选择配置的任务开始调试。javascript: Auto Attach
,这样可以在不切换终端的情况下直接调试Node.js进程。--inspect-brk
标志:node server.js --inspect-brk
chrome://inspect
,点击“为Node打开专用 DevTools”,然后点击“添加连接”并匹配Node.js监听的端口。sudo npm install pm2 -g
node.service
文件并放置在 /etc/systemd/system/
目录下:[Unit]
Description=My super Node.js app
[Service]
WorkingDirectory=/home/root/Heroku/
ExecStart=/usr/bin/node /home/root/Heroku/server.js
Restart=always
RestartSec=500
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs
User=root
Group=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reloadsudo systemctl start node
sudo systemctl enable node
通过以上方法,你可以在Debian系统中有效地调试Node.js应用程序。选择适合你工作流程的方法进行调试。