node-best-practices

A guide to practical Node.js backend development, covering asynchronous code, errors, dependencies, performance, and security.

In plain words
What is it for?
Use it when building Node.js HTTP services, command-line tools, background jobs, middleware, or security and production-readiness checks.
Why use it?
It helps prevent forgotten awaits, blocked requests, inconsistent error handling, unsafe dependencies, and secrets or settings embedded in code.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/wade-devcode/awesome-coding-skills-cn/node-best-practices
Any agent
npx skills add Wade-DevCode/awesome-coding-skills-cn --skill node-best-practices
Clone the repo
git clone --depth 1 https://github.com/Wade-DevCode/awesome-coding-skills-cn

Made for: Claude Code, Codex.

Per session 28 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,655 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00028 $0.02655
Opus 5 $0.00014 $0.01327
Sonnet 5 $0.00006 $0.00531
Haiku 4.5 $0.00003 $0.00265

Measured 2d ago against content hash 35f24c5a5e45, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

node-best-practices scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/node-best-practices/SKILL.md · 178 lines

How it starts

The opening of the file, as written. The whole thing — 178 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Node.js 最佳实践

何时用

  • 写新的 Node.js HTTP 服务、CLI 工具或后台任务时。
  • 处理异步流程、设计中间件错误处理时。
  • 引入新依赖或做安全加固时。
  • Review 代码,发现有混用回调、漏 await、阻塞事件循环或配置硬编码时。
  • 上线前做安全自查时。

核心规则

1. 全程 async/await + try/catch;不混用回调,不漏 await 导致竞态

规则: 所有异步操作统一用 async/await;禁止在 async 函数中混写 callback 风格;每个 await 表达式都必须被 try/catch 覆盖或由调用链上的统一错误处理捕获;await 不能遗漏——没有 await 的 Promise 是悬空的。

为什么: AI 生成 Node.js 代码时最常见的错误:router.get('/user', async (req, res) => { const user = getUser(req.params.id); res.json(user); })——忘记 awaituser 是一个 Promise 对象而非数据,res.json 把 Promise 序列化成 {},请求返回空对象。这类 bug 在简单场景下测试不出来,到了有延迟的生产环境才暴露,且错误信息毫无指向性。

怎么做:

  • 接收到 callback 风格 API(如旧版 fs)→ 用 util.promisify 包一层,再 await
  • 并行等多个无依赖的 Promise → await Promise.all([...]) 而非串行多个 await
  • async 函数内若有 setTimeout/setInterval 回调,注意内部异常不会自动冒泡,需显式 try/catch

2. 不阻塞事件循环;CPU 密集任务用 Worker/队列

规则: 事件循环线程禁止执行耗时超过几毫秒的 CPU 密集操作(JSON 解析大文件、加密运算、图像处理、复杂正则);此类任务交给 worker_threads、独立进程或异步任务队列(BullMQ、Celery 等)处理。

为什么: Node.js 是单线程事件循环,一个同步计算如果耗时 200ms,这 200ms 内所有其他请求都被冻结。AI 常把 JSON.parse(fs.readFileSync('huge.json')) 或同步加密写在请求处理函数里——在压测前完全看不出问题,一旦数据量上去,P99 延迟暴涨,整个服务响应停滞。这是 Node.js 最致命的性能陷阱之一。

怎么做:

  • 大文件解析 → 用流式读取(fs.createReadStream + JSONStream)或 Worker
  • CPU 密集计算(哈希、压缩、图像缩放)→ worker_threads 或独立微服务。
  • 怀疑阻塞 → 用 clinic.js--prof 火焰图定位,不凭感觉优化。

3. 错误统一处理(中间件);未捕获 rejection 要监听并优雅处理

规则: Express/Koa 等框架中必须注册全局错误处理中间件(四参数 (err, req, res, next)),所有路由的异步错误通过 next(err) 或框架的 async wrapper 汇聚到这里;必须监听 process.on('unhandledRejection')process.on('uncaughtException'),记录日志后优雅退出(不静默吞掉,也不忽视)。

为什么: AI 写 Express 时的典型遗漏:每个路由自己 catch 然后 res.status(500).json({error: e.message})——错误格式散落各处,有些路由根本没 catch,未处理的 rejection 让进程悄悄进入不一致状态继续服务请求。unhandledRejection 在 Node.js 15+ 默认会终止进程,但在旧版本只打印警告,AI 生成的代码常假设旧版行为。

怎么做:

  • Express:async 路由用 asyncHandler 包装(自动把 rejection 转成 next(err));最后注册 app.use((err, req, res, next) => { ... }) 统一响应。
  • unhandledRejection → 记录错误、触发优雅关闭(不 process.exit(1) 立即硬停,先排空连接池)。
  • 区分操作错误(用户输入错误、资源不存在,HTTP 4xx)与程序错误(bug,HTTP 5xx),中间件里按类型返回合适的状态码。

Read the full file on GitHub · 178 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 178 lines · 28 tokens per session scan A 35f24c5a5e45

Subscribe to this mod's changes

node-best-practices is a skill published in the GitHub repository Wade-DevCode/awesome-coding-skills-cn (6 stars, last pushed 2mo ago), licensed MIT. It adds 28 tokens to every session and 2,655 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

chinese-git-workflow

国内 Git 平台配置参考——Gitee、Coding.net、极狐 GitLab、CNB 的 SSH/HTTPS/凭据/CI 接入差异与镜像同步配置。仅在用户显式 /chinese-git-workflow 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 69 tokens

brainstorming

在任何创造性工作之前必须使用此技能——创建功能、构建组件、添加功能或修改行为。在实现之前先探索用户意图、需求和设计。.

jnMetaCode/superpowers-zh · 40 tokens

chinese-code-review

中文 review 沟通参考——话术模板、分级标注(必须修复/建议修改/仅供参考)、国内团队常见反模式应对。仅在用户显式 /chinese-code-review 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-commit-conventions

中文 commit 与 changelog 配置参考——Conventional Commits 中文适配、commitlint/husky/commitizen 中文模板、conventional-changelog 中文配置。仅在用户显式 /chinese-commit-conventions 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 65 tokens

chinese-documentation

中文文档排版参考——中英文空格、全半角标点、术语保留、链接格式、中文文案排版指北约定。仅在用户显式 /chinese-documentation 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

systematic-debugging

Skill "systematic-debugging" from jnMetaCode/superpowers-zh, covering 系统化调试, 概述, 铁律, 何时使用 and 四个阶段.

jnMetaCode/superpowers-zh · 24 tokens