在 Ubuntu 上“读懂 Node.js 日志”,一般可以从 日志来源 → 日志位置 → 日志内容 → 常见错误含义 → 排查方法 这个思路来。下面一步一步说。
Node.js 本身不会自动写系统日志,日志来源通常是:
应用自己打印的日志
console.log()console.error()进程管理器日志
Ubuntu 系统日志
/var/log/syslogjournalctlWeb 服务器 / 反向代理
/var/log/nginx//var/log/apache2/如果你用 systemd 管理 Node 服务:
journalctl -u your-node-service.service -f
常用参数:
journalctl -u xxx.service -n 100 # 最近100行
journalctl -u xxx.service --since "10 min ago"
pm2 logs
pm2 logs app-name
日志文件位置:
~/.pm2/logs/
例如:
node app.js > app.log 2>&1
查看:
tail -f app.log
Node 报错但请求失败,常看 Nginx:
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
INFO:正常信息WARN:可能问题ERROR:真实错误UnhandledPromiseRejection:Promise 没 catchTypeError / ReferenceError:代码问题Error: listen EADDRINUSE: address already in use :::3000
✅ 解决:
lsof -i:3000
kill -9 PID
Error: Cannot find module 'express'
✅ 解决:
npm install
UnhandledPromiseRejectionWarning
✅ 代码里加:
process.on('unhandledRejection', e => {
console.error(e);
});
connect() failed (111: Connection refused)
✅ Node 没启动 / 端口不对
const winston = require('winston');
# 1. 看服务状态
systemctl status node-app
# 2. 看实时日志
journalctl -u node-app -f
# 3. 看端口
netstat -tlnp | grep node
# 4. 看 Nginx
tail -f /var/log/nginx/error.log
如果你愿意,可以把一段真实的 Node.js Ubuntu 日志贴出来,我可以帮你逐行解读并告诉你问题在哪、怎么修。