看懂 Node.js 崩溃日志,可以按“先看结论 → 再找堆栈 → 分析上下文 → 定位代码”的顺序来。下面用实用方式讲清楚。
一个典型的 Node.js 崩溃日志示例:
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
Error: something went wrong
at foo (/app/index.js:10:15)
at bar (/app/index.js:6:3)
at Object.<anonymous> (/app/index.js:2:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
Error: something went wrong
TypeError: Cannot read properties of undefined
RangeError: Maximum call stack size exceeded
常见类型:
| 类型 | 含义 |
|---|---|
Error |
通用错误 |
TypeError |
类型不对(最常见) |
ReferenceError |
变量未定义 |
RangeError |
栈溢出 / 参数越界 |
SyntaxError |
代码写错(一般启动就崩) |
Cannot read properties of undefined (reading 'name')
翻译一下:
你访问了
undefined.name
at foo (/app/index.js:10:15)
at bar (/app/index.js:6:3)
含义:
foo 是在 index.js 第 10 行调用的bar 调用了 foo✅ 重点看 第一行你的代码
如果是异步错误,常见形式:
UnhandledPromiseRejectionWarning
说明:
✅ 解决方式:
process.on('unhandledRejection', e => {
console.error(e);
});
SIGSEGV
Segmentation fault
说明:
at 你的文件at UserService.getUser (/src/user.js:25:10)
直接打开那一行。
undefined 吗?await?console.log(JSON.stringify(data));
| 日志 | 原因 |
|---|---|
Cannot read property of undefined |
没判空 |
await is only valid in async |
函数没 async |
ECONNRESET |
网络断了 |
ETIMEDOUT |
请求超时 |
Maximum call stack |
递归没退出 |
Error.prepareStackTrace
console.error({
msg: 'db error',
err
});
如果你愿意:
我可以直接帮你逐行翻译成人话。