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...

How does JWT Authorization works

When a user logs into the application, they provide their username and password to the backend. The backend verifies these credentials and, if they are valid, generates a JSON Web Token (JWT) containing information such as the user's ID and roles. This JWT is signed with a secret key unique to the server and sent back to the client. The client securely stores the JWT, commonly in browser storage or as an HTTP-only cookie. When the user attempts to access protected resources or perform specific tasks, the client sends the JWT with the request to the backend. The backend then verifies the integrity of the token by checking its signature. If the signature is valid, the backend extracts the user information from the JWT payload and processes the request accordingly, ensuring that the user has the necessary permissions based on their roles and authorization level. Additionally, JWTs often have an expiration time, after which the token becomes invalid, prompting the user to re-authentica...

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 ...