在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"
}
]
}
启动调试:
在 VSCode 中按 F5
或点击左侧调试图标,选择配置的任务开始调试。
使用自动附加:
在设置中启用 javascript: Auto Attach
,这样可以在不切换终端的情况下直接调试 Node.js 进程。
--inspect-brk
标志:node server.js --inspect-brk
在 Chrome 浏览器中打开 DevTools:
访问 chrome://inspect
,点击“为 Node 打开专用 DevTools”,然后点击“添加连接”并匹配 Node.js 监听的端口。
调试异常和设置断点: 在 DevTools 中,可以使用调试工具栏中的按钮暂停执行,检查变量值,设置断点等。
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-reload
sudo systemctl start node
sudo systemctl enable node
以上方法可以帮助你在Debian系统中有效地调试Node.js应用程序。选择适合你工作流程的方法进行调试。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>