在 Debian 上排查 Node.js 日志和异常,通常可以从下面几个方面入手:
不同运行方式,日志位置不一样:
node app.js
异常会直接输出在终端。
pm2 logs
pm2 logs appname
pm2 describe appname # 看日志路径
日志一般在:
~/.pm2/logs/
查看服务名:
systemctl list-units --type=service | grep node
查看日志:
journalctl -u your-node-service.service -f
journalctl -u your-node-service.service --since "2024-01-01"
日志示例:
Error: Cannot find module 'xxx'
at Function.Module._resolveFilename
重点看:
Error: 后面内容at ... 调用栈UnhandledPromiseRejectionWarning
解决方式:
process.on('unhandledRejection', (reason) => {
console.error('Unhandled Rejection:', reason);
});
EADDRINUSE: address already in use :::3000
查看占用:
sudo lsof -i:3000
# 或
ss -ltnp | grep 3000
process.on('uncaughtException', (err) => {
console.error('uncaughtException:', err);
});
winstonpinobunyan示例(winston):
const winston = require('winston');
winston.error('db connect failed', err);
top
free -h
df -h
dmesg | grep -i "killed process"
journalctl -k | grep -i oom
pm2 logs 或 journalctl -f)Error / Exception / Reject / EADDRINUSE如果你愿意,可以把具体的报错日志或**运行方式(pm2 / docker / systemd)**发出来,我可以直接帮你分析是哪一类问题。