下面按实战流程给你一个比较系统的 Node.js 日志报错排查思路,从“看现象”到“定位根因”。
典型:
SyntaxError: Unexpected token
Error: Cannot find module 'xxx'
✅ 排查点:
node -v 版本是否支持该语法npm installTypeError: Cannot read properties of undefined
ReferenceError: xxx is not defined
✅ 排查点:
null / undefined 未做判断UnhandledPromiseRejectionWarning
✅ 排查点:
await 了 Promisetry/catchprocess.on('unhandledRejection', e => console.error(e))
Error: listen EADDRINUSE
Segmentation fault
✅ 排查点:
示例:
Error: something wrong
at getUser (/app/user.js:23:15)
at async handle (/app/router.js:10:5)
✅ 重点看:
console.log(JSON.stringify(data, null, 2))
node --inspect app.js
或:
debugger;
DEBUG=* node app.js
(很多库支持 DEBUG)
process.on('uncaughtException', e => {
console.error('FATAL', e)
})
不要用 console.log 生产环境:
winstonpinolog4js| 场景 | 可能原因 |
|---|---|
| 接口 500 | 未捕获 async 错误 |
| 偶发崩溃 | 内存泄漏 |
| 启动慢 | 依赖过多 |
| CPU 100% | 死循环 / 正则灾难 |
| 请求卡死 | 未释放连接 |
你可以直接贴:
我可以逐行帮你定位问题。