Skip to main content

Command Palette

Search for a command to run...

🚀Step-by-Step Guide to Building an Express App with TypeScript

Updated
4 min read
🚀Step-by-Step Guide to Building an Express App with TypeScript

When starting with Node.js development, one of the best setups is combining Express (for building APIs and web apps) with TypeScript (for type safety and developer experience). In this post, we’ll go step by step to set up a brand-new TypeScript + Express project from scratch.


🛠️ Prerequisites

  • Node.js (v16+ recommended)

  • npm (comes with Node.js)

  • A code editor (VS Code suggested)

⚡ Step 1: Allow PowerShell to Run npm

If you’re on Windows, you may run into a script signing error when trying to use npm. To temporarily bypass it for your current session, run:

mkdir demo
cd demo
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

This won’t change system-wide settings; it only works until you close the PowerShell window.


⚡ Step 2: Initialize a New Node.js Project

Inside your project folder, initialize npm:

npm init -y

This creates a package.json file with default values, which will track your dependencies and scripts.

{
  "name": "demo",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "ts-node src/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^5.1.0"
  },
  "devDependencies": {
    "@types/express": "^5.0.3",
    "@types/node": "^24.3.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.9.2"
  }
}

⚡ Step 3: Install Express

Next, add Express.js:

npm i express

Express is a minimal and flexible web framework for Node.js that makes it easy to create APIs and web applications.

It creates node_modules folder with required modules and creates package-lock.json

⚡ Step 4: Install TypeScript and Developer Tools

Since we’re working with TypeScript, we need some dev dependencies:

npm install --save-dev typescript ts-node @types/node @types/express
  • typescript → TypeScript compiler

  • ts-node → Run .ts files directly without compiling manually

  • @types/node → Type definitions for Node.js core modules

  • @types/express → Type definitions for Express

⚡ Step 5: Configure TypeScript

Initialize TypeScript in the project:

npx tsc --init

This creates a tsconfig.json file. You can adjust it later, but a good starting point is to set:

{
  // Visit https://aka.ms/tsconfig to read more about this file
  "compilerOptions": {
    // File Layout
    "rootDir": "./src",
    "outDir": "./dist",

    // Environment Settings
    // See also https://aka.ms/tsconfig/module
    "module": "nodenext",
    "target": "esnext",
    "types": [],
    // For nodejs:
    // "lib": ["esnext"],
    // "types": ["node"],
    // and npm install -D @types/node

    // Other Outputs
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,

    // Stricter Typechecking Options
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,

    // Style Options
    // "noImplicitReturns": true,
    // "noImplicitOverride": true,
    // "noUnusedLocals": true,
    // "noUnusedParameters": true,
    // "noFallthroughCasesInSwitch": true,
    // "noPropertyAccessFromIndexSignature": true,

    // Recommended Options
    "strict": true,
    "jsx": "react-jsx",
    "verbatimModuleSyntax": true,
    "isolatedModules": true,
    "noUncheckedSideEffectImports": true,
    "moduleDetection": "force",
    "skipLibCheck": true,
  }
}

⚡ Step 6: Create Your First Express Server

Inside a new src folder, create a file index.ts:

const express = require("express");
import type { Request, Response } from "express";

const app = express();
const port = 3000;

// Middleware
app.use(express.json());

// Routes
app.get("/", (req: Request, res: Response) => {
  res.send("Hello, TypeScript + Express!");
});
app.get("/test", (req: Request, res: Response) => {
    res.status(200).json({
        message: "Finally deployment and cicd pipelined done!",
        version: "1.0.0",
        author: "ss",
        date: new Date().toISOString(),
    });
});
// Start server
app.listen(port, () => {
  console.log(`🚀 Server running at http://localhost:${port}`);
});

⚡ Step 7: Add a Start Script

Update your package.json scripts section:

{
  "name": "project-1",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "ts-node src/index.ts",
    "build": "tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^5.2.1"
  },
  "devDependencies": {
    "@types/express": "^5.0.6",
    "@types/node": "^24.10.1",
    "ts-node": "^10.9.2",
    "typescript": "^5.9.3"
  }
}

⚡ Step 8: Run Your App

Now, start your server:

 npm run start

Open your browser and navigate to:

http://localhost:3000

You should see:

Hello, TypeScript + Express!

Dockerfile

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
services:
  app:
    build: .
    ports:
      - "3000:3000"

  nginx:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    depends_on:
      - app

nginx.conf

server {
   listen 80;

   location / {
      proxy_pass http://app:3000;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
   }
}

🎉 Conclusion

You now have a working Express + TypeScript project setup! From here, you can:

  • Add more routes and controllers

  • Connect to a database (MongoDB, PostgreSQL, MySQL, etc.)

  • Write unit tests with Jest or Mocha

  • Containerize your app with Docker

More from this blog

thiru's blog

43 posts