Node.js 崩溃(Crash)通常不是普通异常,而是进程直接退出(如 OOM、段错误、未捕获异常等)。定位崩溃日志和原因,可以按下面步骤来。
常见日志:
Error: xxx
at ...
node:internal/process/uncaughtException
✅ 原因:
throw 没 catchpromise 没 .catch✅ 解决:
process.on('uncaughtException', (err) => {
console.error('uncaught', err);
});
process.on('unhandledRejection', (reason) => {
console.error('unhandledRejection', reason);
});
日志通常含:
FATAL ERROR: Ineffective mark-compacts near heap limit
Allocation failed - JavaScript heap out of memory
✅ 定位:
✅ 工具:
node --inspect app.js
用 Chrome DevTools → Memory → Heap Snapshot
日志:
Segmentation fault (core dumped)
✅ 常见原因:
node-gyp 编译版本不匹配sharp, bcrypt)✅ 定位:
node --abort-on-uncaught-exception
或生成 core dump 分析
process.on('uncaughtException', (err) => {
fs.writeFileSync('crash.log', err.stack);
process.exit(1);
});
ulimit -c unlimited
node app.js
崩溃后:
gdb node core.xxx
bt
--stack-trace-limitnode --stack-trace-limit=100 app.js
pm2 start app.js
pm2 logs
pm2 describe <id>
PM2 会自动:
Sentry.init({ dsn: '...' });
能捕获:
| Code | 含义 |
|---|---|
| 1 | 未捕获异常 |
| 137 | OOM(内存超限) |
| 139 | Segmentation fault |
| 132 | Illegal instruction |
uncaughtException如果你愿意,可以把崩溃日志原文贴出来,我可以直接帮你定位是哪一类问题。