Chapter 22: TypeScript with Node.js

TypeScript with Node.js like we’re sitting together in a quiet Hyderabad café, laptop open, terminal ready, and no hurry at all. I’ll explain everything step-by-step, very practically, with real 2025–2026 patterns that people actually use in production (Express APIs, NestJS backends, CLI tools, serverless functions, etc.).

1. What does “TypeScript with Node.js” really mean?

  • You write your backend / server / script / CLI code in TypeScript (.ts / .tsx files)
  • You get static types, interfaces, generics, better refactoring, autocompletion, fewer runtime surprises
  • At the end → TypeScript compiler (tsc) or a runner turns your .ts into plain JavaScript (.js) that Node.js can execute
  • Node.js itself does not understand TypeScript — it only runs JavaScript (or increasingly experimental TypeScript in very recent versions)

In 2026, there are several realistic ways to run TypeScript code in Node.js:

Way How it works Best for Production ready? Speed / DX in 2026
Compile → run JS (tsc + node dist/index.js) Classic: build step → output .js Almost all production servers ★★★★★ Good build, good run
ts-node / tsx runner Run .ts directly (transpiles on-the-fly) Development, scripts, quick prototypes ★★★★ Very convenient
Node.js native –experimental-strip-types (Node 22+ / 23+) Node runs .ts files almost natively (very new) Very light dev setups ★★☆ (2026 still experimental) Fastest dev experience
bun / deno runtime They understand TS out-of-the-box If you’re open to non-Node runtimes ★★★★ Excellent DX

Most serious production Node.js + TS projects in 2026 still use compile → run JS (way #1) because it’s stable, debuggable, works with every tool (Docker, PM2, Kubernetes, Vercel, etc.).

2. Modern setup in 2026 (ESM + TypeScript — recommended for new projects)

Let’s build a tiny Express API together — this is the most common real-world pattern right now.

Step 1: Initialize project

Bash

Step 2: Install dependencies

Bash

package.json — make it ESM (very important in 2026)

JSON

Step 3: Create tsconfig.json (modern ESM-friendly 2026 settings)

JSON

Step 4: First file — src/index.ts

TypeScript

Step 5: Run it

Bash

3. Why ESM + NodeNext in 2026?

  • Better tree-shaking
  • Top-level await support
  • Consistent with frontend (Vite, Next.js, etc.)
  • Future-proof (CommonJS is legacy now)
  • But… requires explicit .js extensions in imports if you compile to JS:
TypeScript

Many teams use tsx in dev (it handles extensions automatically) and compile to .js for production.

4. Quick comparison: CommonJS vs ESM in Node + TS (2026 reality)

Aspect CommonJS (“module”: “commonjs”) ESM (“module”: “NodeNext”, “type”: “module”)
Syntax require(), module.exports import, export
File extensions needed No Yes (in compiled JS)
Top-level await No Yes
New projects (2026) Legacy / migration cases Strongly recommended
Interop with old libs Easier Sometimes needs await import()
tsconfig setting “module”: “commonjs”, “moduleResolution”: “node” “module”: “NodeNext”, “moduleResolution”: “NodeNext”

Recommendation 2026: New project → ESM + NodeNext Legacy / quick migration → CommonJS

5. Very common real patterns in 2026 Node + TS projects

  • Environment variables → dotenv + zod for validation
  • Error handling → Custom AppError class + middleware
  • Async/await everywhere → express-async-errors or try/catch wrappers
  • Type-safe routes → Libraries like express-zod-api, tRPC, NestJS
  • Testing → vitest or jest + @types/jest
  • Linting → ESLint flat config + Prettier + typescript-eslint

6. Mini homework (try today!)

  1. Create the project above
  2. Add one POST route /api/users that accepts JSON → validates with interface → adds to array
  3. Try npm run dev → add a user → GET /api/users in browser/Postman
  4. Intentionally pass wrong type → see TypeScript error before runtime

Any part confusing? Want to zoom into:

  • Full NestJS + TypeScript setup
  • tRPC / Fastify instead of Express
  • Docker / deployment tips
  • Handling ESM pain points (extensions, dual packages)
  • Strict mode flags for backend safety

Just tell me — we’ll go deeper right there! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *