分析 Node.js 崩溃日志(crash logs)通常是为了定位为什么进程退出、在哪一行代码出错、以及如何修复。下面按「从现象到根因」的思路系统讲一下。
Uncaught Error: something wrong
at app.js:10:5
UnhandledPromiseRejectionWarning: Error: xxx
Segmentation fault (core dumped)
FATAL ERROR: Ineffective mark-compacts near heap limit
exit code 137(OOM Killer)以典型异常为例:
node:internal/process/esm_loader:74
throw new Error('xxx');
^
Error: xxx
at foo (/app/app.js:12:10)
at bar (/app/app.js:20:5)
at Object.<anonymous> (/app/app.js:30:1)
重点看:
Error / TypeError / RangeErrorECONNRESET / ETIMEDOUT(网络)Cannot read property of undefinednode:internal| 退出码 | 含义 |
|---|---|
| 1 | 普通错误 |
| 7 | 未捕获异常 |
| 137 | 被 kill(OOM) |
| 139 | Segmentation fault |
原因
try/catch分析
process.on('uncaughtException', (err) => {
console.error('CRASH', err);
});
✅ 建议:
UnhandledPromiseRejectionWarning: Error: xxx
原因
async 函数没 await.then() 没 .catch()分析
process.on('unhandledRejection', (reason) => {
console.error(reason);
});
✅ 建议:
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed
分析
工具
node --max-old-space-size=4096 app.js
node --inspect app.js
✅ 用 Chrome DevTools 看 Memory 快照
Segmentation fault (core dumped)
原因
分析
node --abort-on-uncaught-exception
gdb node core
process.on('uncaughtException', e => {
fs.writeFileSync('crash.log', e.stack);
process.exit(1);
});
ulimit -c unlimited
node --abort-on-uncaught-exception app.js
node --stack-trace-limit=100 app.js
requestId如果你愿意,可以把真实的崩溃日志贴出来(脱敏后),我可以直接帮你定位是哪一类问题、怎么改。