Quick Reference
| Topic | File |
|---|
| Callbacks, Promises, async/await, event loop | INLINECODE0 |
| CommonJS vs ESM, require vs import |
modules.md |
| Error handling, uncaught exceptions |
errors.md |
| Readable, Writable, Transform, backpressure |
streams.md |
| Memory leaks, event loop blocking, profiling |
performance.md |
| Input validation, dependencies, env vars |
security.md |
| Jest, Mocha, mocking, integration tests |
testing.md |
| npm, package.json, lockfiles, publishing |
packages.md |
Critical Traps
- -
fs.readFileSync blocks entire server — use INLINECODE9 - Unhandled rejection crashes Node 15+ — always
.catch() or try/catch - INLINECODE11 values are strings —
"3000" not 3000, parseInt needed - INLINECODE14 throws on invalid — wrap in try/catch
- INLINECODE15 cached — same object, mutations visible everywhere
- Circular deps return incomplete export — restructure to avoid
- Event listeners accumulate —
removeListener or INLINECODE17 - INLINECODE18 always returns Promise — even for plain return
- INLINECODE19 over
.pipe() — handles errors and cleanup - No
__dirname in ESM — use INLINECODE22 - INLINECODE23 — encoding matters, default UTF-8
快速参考
| 主题 | 文件 |
|---|
| 回调、Promise、async/await、事件循环 | async.md |
| CommonJS与ESM对比、require与import |
modules.md |
| 错误处理、未捕获异常 | errors.md |
| 可读流、可写流、转换流、背压 | streams.md |
| 内存泄漏、事件循环阻塞、性能分析 | performance.md |
| 输入验证、依赖项、环境变量 | security.md |
| Jest、Mocha、模拟、集成测试 | testing.md |
| npm、package.json、锁文件、发布 | packages.md |
关键陷阱
- - fs.readFileSync 会阻塞整个服务器 — 应使用 fs.promises.readFile
- 未处理的拒绝会导致 Node 15+ 崩溃 — 始终使用 .catch() 或 try/catch
- process.env 的值都是字符串 — 3000 而非 3000,需要使用 parseInt
- JSON.parse 在无效输入时会抛出异常 — 需包裹在 try/catch 中
- require() 会缓存结果 — 同一对象,所有地方的修改都可见
- 循环依赖会返回不完整的导出 — 需重构代码结构以避免
- 事件监听器会不断累积 — 使用 removeListener 或 once()
- async 始终返回 Promise — 即使是普通返回值也是如此
- pipeline() 优于 .pipe() — 能处理错误和清理工作
- ESM 中没有 dirname — 使用 fileURLToPath(import.meta.url)
- Buffer.from(string) — 编码很重要,默认使用 UTF-8