Skip to main content

How to Set Up an Express Project with TypeScript, Step-by-Step Guide


Introduction

Setting up an Express project with TypeScript can enhance your development experience by providing type safety and better tooling. In this guide, we’ll walk through the steps to create a simple Express application using TypeScript.

Prerequisites

Before we begin, ensure you have the following installed on your machine:

  • Node.js: You can download it from nodejs.org.
  • npm (Node Package Manager) comes with Node.js.

Step 1: Initialize Your Project

First, create a new directory for your project and navigate into it:


mkdir my-express-ts-app cd my-express-ts-app

Now, initialize a new Node.js project:

npm init -y

This command will create a package.json file in your project directory.

Step 2: Install Dependencies

Next, install Express and TypeScript along with the necessary types:


npm install express npm install --save-dev typescript @types/node @types/express ts-node
  • express: The web framework.
  • typescript: The TypeScript compiler.
  • @types/node: Type definitions for Node.js.
  • @types/express: Type definitions for Express.
  • ts-node: A TypeScript execution engine for Node.js.

Step 3: Create TypeScript Configuration File

Create a TypeScript configuration file named tsconfig.json:

npx tsc --init

Modify the tsconfig.json file to include the following options:


{ "compilerOptions": { "target": "ES6", "module": "commonjs", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "outDir": "./dist", "rootDir": "./src" }, "include": ["src/**/*"], "exclude": ["node_modules"] }

Step 4: Create Project Structure

Create the following directory structure:


my-express-ts-app ├── src │ └── index.ts ├── package.json └── tsconfig.json

Step 5: Set Up Your Express Server

In the src/index.ts file, add the following code:


import express from 'express'; const app = express(); const PORT = process.env.PORT || 3000; app.get('/', (req, res) => { res.send('Hello, TypeScript with Express!'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });

Step 6: Update Scripts in package.json

Modify your package.json to include the following scripts:


"scripts": { "build": "tsc", "start": "node dist/index.js", "dev": "ts-node src/index.ts" }
  • build: Compiles TypeScript files into JavaScript.
  • start: Runs the compiled JavaScript.
  • dev: Runs the TypeScript files directly.

Step 7: Run Your Application

To run your application in development mode, use the following command:


npm run dev

You should see the output indicating that the server is running. Open your browser and navigate to http://localhost:3000 to see the message "Hello, TypeScript with Express!"

Step 8: Build and Run in Production

To build your project for production, run:


npm run build

Then, start the application:


npm start

Congratulations! You've successfully set up an Express project with TypeScript. This setup will give you a robust foundation for building scalable applications with type safety. You can now start developing your APIs and services with confidence.


Comments

Popular posts from this blog

Book Review - "A History of Western Philosophy" – A Personal Review and Reflection

Reading Bertrand Russell’s A History of Western Philosophy felt like stepping into the grand, winding corridors of Western thought — with Russell himself as your witty, insightful, and occasionally snarky guide. I didn’t open this book expecting to agree with everything. What I wanted was clarity, structure, and a sense of how all these big thinkers across the centuries connect. And that’s exactly what this book delivered, with a tone that balances intellect and irreverence brilliantly. Russell doesn’t just list philosophers. He sketches their ideas, yes, but also their lives, their contradictions, their blind spots. From the ancient Greeks to the modern rationalists, he doesn’t hesitate to praise where it's due, but he also critiques with surgical precision. He brings Plato, Aristotle, Aquinas, Descartes, Spinoza, Hume, Kant, Nietzsche — and so many more — into sharp, readable focus. What makes this book stand out to me is that Russell writes as both a philosopher and a histor...

Book Review - "The Selfish Gene" – A Personal Review and Reflection

Some books explain things. Others  reframe  how you understand reality. Richard Dawkins’  The Selfish Gene  definitely falls into the second category. I went in expecting a book about biology — what I got was a radical shift in how I think about life, evolution, and even human behavior. Right from the opening, Dawkins does something bold: he asks us to stop thinking of evolution as being about individuals or species, and to focus instead on genes. Genes, he argues, are the real players in evolution — long-lasting replicators, using bodies as temporary vehicles to get themselves copied. This shift from the organism to the gene as the central unit of natural selection is what makes the book so groundbreaking. Now, the term “selfish” isn’t meant morally — it’s metaphorical. Genes aren’t conscious or evil. But they “behave,” in an evolutionary sense, as if they’re selfish — doing whatever increases their chances of being passed on. That means creating organisms that are ...

Book Review - "Sapiens: A Brief History of Humankind" – A Personal Review and Reflection

Reading Sapiens by Yuval Noah Harari felt like zooming way, way out from my daily life and looking at the human story from the edge of the galaxy. It’s ambitious, thought-provoking, and at times, deeply unsettling — in the best way possible. It made me question the narratives I’ve lived by, the institutions I trust, and the assumptions I didn’t even know I was carrying. The book covers an enormous timeline, from the cognitive revolution about 70,000 years ago to the present digital age. But Harari doesn’t just recount facts — he interprets them. He explores how Homo sapiens became the dominant species not because we were the strongest, but because we could cooperate in large groups based on shared myths — religion, money, nations, human rights. That insight alone made me stop and reevaluate how powerful stories really are in shaping our world. One of the most fascinating ideas in the book is that our success came not just from intelligence, but from our ability to believe in fiction...