Skip to content

Node.js and TypeScript back ends

Scope: server-side services that run on Node.js and are written in TypeScript, such as HTTP APIs, workers and scheduled jobs. For configuration, health checks, telemetry and resilience, follow general/operations-reliability.md; for secrets, authorization and the supply chain, follow general/security.md. The rules here cover what is specific to Node.js and TypeScript.

Entries are sorted by score from 1 to 10 (criteria): 9โ€“10 start here or strongly recommended, 6โ€“8 recommended, 1โ€“5 further reading or conditional.

Adopted references

  • 9 Don't Block the Event Loop (or the Worker Pool) (Node.js): how Node.js runs many clients on a few threads, and how to keep each callback and task small. Read it before designing a service.
  • 9 Security Best Practices (Node.js): Node.js-specific threats, such as HTTP denial of service, malicious dependencies and prototype pollution, with mitigations for each.
  • 7 Node.js Best Practices (Yoni Goldberg and contributors): the largest community collection, covering project structure, error handling, testing, production, security and Docker. Use it as a checklist; it isn't an official source.

Key rules

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 TypeScript strict: with strict on, running tsc --noEmit in CI fails the build on type errors. Checks NODE-8.
  • 7 typescript-eslint no-floating-promises and no-misused-promises: type-aware lint rules, both in recommended-type-checked, that flag unhandled promises and promises passed where they aren't handled. Checks NODE-2, and partly PRIN-16.
  • 7 npm ci and npm audit: npm ci fails when package.json and the lockfile disagree, and npm audit --audit-level=<level> fails on known vulnerabilities. Pair them with Dependabot (general/security.md). Partly checks NODE-4.
  • 6 eslint-plugin-n no-sync: flags calls to synchronous *Sync methods. Partly checks NODE-1.
  • 6 eslint-plugin-regexp no-super-linear-backtracking: flags regular expressions with exponential or polynomial backtracking (ReDoS), which block the event loop. Partly checks NODE-1.
  • 6 dependency-cruiser: forbidden rules with group matching stop one business component from importing another's internals, and fail CI on violations. Partly checks NODE-7.

Tutorials and further reading

  • 9 The TypeScript Handbook (TypeScript): the official guide to the language and its type system.
  • 7 Node.js Design Patterns (Mario Casciaro and Luciano Mammino, 4th edition): asynchronous programming, streams, design patterns, testing, and scaling with microservices and messaging.