Lesson

TypeScript with Node.js

10 min reading
Free Course

TypeScript with Node.js: Backend Setup and Type-Safe APIs

Using TypeScript with Node.js brings compile-time type safety to backend routes, database queries, environment configs, and CLI scripts. Instead of discovering a typo like req.body.emial when an endpoint fails in production, your editor catches it immediately.

Node.js setups need @types/node so TypeScript recognizes built-in globals and modules like process, Buffer, node:fs, and node:path.

Minimal Backend Setup and Example

Install TypeScript, @types/node, and tsx for fast local execution without manual build steps:

npm install -D typescript @types/node tsx

Always import Node core modules with the node: prefix (node:path, node:fs/promises) so readers and bundlers distinguish standard Node APIs from npm packages.

import { readFile } from "node:fs/promises";
import path from "node:path";

interface AppConfig {
  port: number;
  env: "development" | "production";
}

async function loadConfig(filePath: string): Promise<AppConfig> {
  const fullPath = path.resolve(filePath);
  const raw = await readFile(fullPath, "utf-8");
  return JSON.parse(raw) as AppConfig;
}

console.log(typeof loadConfig);

Choosing moduleResolution for Node.js

If you compile with tsc and run node dist/index.js directly in ESM mode, set "module": "NodeNext" and "moduleResolution": "NodeNext" in tsconfig.json. If you bundle with tsup or esbuild, "moduleResolution": "bundler" works without .js import extensions.

Common Mistakes to Avoid

  • Forgetting @types/node: Without @types/node, process.env, __dirname, and node:fs trigger missing-name errors.
  • Trusting process.env.PORT as a number: Every environment variable in process.env is string | undefined; parse and validate env vars at startup.
  • Using .ts extensions in imports without allowImportingTsExtensions: Under NodeNext, relative imports use .js extensions even though source files end in .ts.

Frequently Asked Questions

Can modern Node.js run TypeScript files natively without tsc?

Yes. Node.js 22.6+ supports --experimental-strip-types (enabled by default in Node 23.6+), running erasable TypeScript syntax directly while ignoring enum and parameter properties.

Why use tsx in development?

tsx runs .ts files, handles path aliases, and restarts automatically with tsx watch src/server.ts in milliseconds.

How should I compile a Node.js TypeScript project for production Docker images?

Run tsc (or tsup) during the Docker build stage to emit clean .js files into dist/, then run node dist/server.js in production with only production dependencies installed.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
TypeScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum