How to use TypeScript with Node.js

· Category: TypeScript

Short answer

Write your Node.js application in .ts files, install @types/node for built-in module types, and run with ts-node, tsx, or a compiled outDir.

Steps

  1. Install TypeScript and Node types: npm install -D typescript @types/node
  2. Set module and moduleResolution appropriately (CommonJS or NodeNext).
  3. Set target to a supported ECMAScript version for your Node.js runtime.
  4. Use ts-node for development: ts-node src/index.ts.
  5. Compile for production: tsc and run node dist/index.js.

Tips

  • tsx is a faster modern alternative to ts-node that uses esbuild.
  • Use NODE_ENV types with declaration merging on ProcessEnv for autocomplete.

Common issues

  • Missing @types/node causes errors on require, process, and Buffer.
  • Mismatched module settings between TypeScript and your runtime lead to import errors.