Setting up TypeScript with Node.js and Express gives you the benefits of strong typing and better tooling while building your backend server. Here's a step-by-step guide to get you started quickly.
How to Set Up TypeScript with Node.js and Express
1. Initialize Your Project
Open your terminal and create a new directory for your project:
mkdir my-express-ts-app
cd my-express-ts-app
npm init -y
This creates a package.json with default settings.
2. Install Dependencies
Runtime dependencies:
npm install express
Development dependencies:
npm install -D typescript ts-node @types/node @types/express nodemon
typescript — TypeScript compiler
ts-node — Runs TS files directly
@types/node — Node.js type definitions
@types/express — Express type definitions
nodemon — Automatically restarts server on code changes
3. Initialize TypeScript Configuration
Run:
npx tsc --init
This creates a tsconfig.json file.
Recommended changes to tsconfig.json:
Make sure these options are set:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
4. Create Project Structure
mkdir src
touch src/index.ts
5. Create Express Server in TypeScript (src/index.ts)
import express, { Request, Response } from 'express';
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req: Request, res: Response) => {
res.send('Hello from Express + TypeScript!');
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
6. Add Scripts to package.json
Modify package.json to add:
"scripts": {
"start": "node dist/index.js",
"build": "tsc",
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts"
}
npm run dev → Run server in development mode with auto reload
npm run build → Compile TypeScript to JavaScript
npm start → Run compiled JavaScript from dist/
7. Run Your Server
Start development server:
npm run dev
You should see:
Server is running at http://localhost:3000
Visit http://localhost:3000 in your browser and see the message.
Done!
You now have a basic Node.js + Express server fully set up with TypeScript.