How to use express in typescript ?
Last Updated :
11 Oct, 2022
In this article, we will see how to use Express in TypeScript. The TypeScript is a superset of JavaScript that provides type notation with the JavaScript, so we can handle our server, and very scalable for the future. Express is web framework helps to create server-side handling with the help of Nodejs
Prerequisite:
- Basic knowledge of NodeJS and Express.
- Basic knowledge of TypeScript.
Steps to create an Express server in Typescript:
Step 1: Initiate the package.json file with your working folder with the following command.
npm init -y
Note: The '-y' flag is used to make the default configuration.
After creating package.json file, the following will be the output.

Step 2: Install the required modules using the following command.
- Add an express module for our server.
npm install express
- Add typescript and ts-node for run typescript on the NodeJS.
npm i typescript ts-node nodemon --save-dev
Note: The '--save-dev' is used to add dev dependency.
npm i @types/node @types/express
Step 3: Create a tsconfig.json file with the below code.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"rootDir": "./",
"outDir": "./build",
"esModuleInterop": true,
"strict": true
}
}
Step 4: Make the following change in the package.json file to run typescript directly.

Step 5: Create an index.ts file with the following code. Here we have created a minimal express server in typescript. Here, file name is index.ts.
JavaScript
// Import the express in typescript file
import express from 'express';
// Initialize the express engine
const app: express.Application = express();
// Take a port 3000 for running server.
const port: number = 3000;
// Handling '/' Request
app.get('/', (_req, _res) => {
_res.send("TypeScript With Express");
});
// Server setup
app.listen(port, () => {
console.log(`TypeScript with Express
http://localhost:${port}/`);
});
Step 6: Start the server using the below command.
npm start
Output: We will see the following output on the terminal screen.

Step 7: Open the browser and go to http://localhost:3000, we will see the following output.
Â

Reference:
Similar Reads
How to Use Fetch in TypeScript ? In TypeScript, fetching the data from any server or API using fetch() API is not the same as we used to do it in Plain or Vanilla JavaScript. Let us see how we can use the fetch API to fetch data from any server or API in TypeScript.NOTE: Before using this code, please make sure that you have jQuery
4 min read
How to use TypeScript on backend ? TypeScript, developed by Microsoft, enhances JavaScript by adding type safety, which significantly simplifies debugging and development. This guide will walk you through setting up a TypeScript backend with Node.js and Express, showing you how to leverage TypeScriptâs robust features to prevent comm
2 min read
How to Use NextJS in Typescript? TypeScript enhances Next.js applications by adding static type checking and improves developer experience through type safety. Integrating TypeScript into your Next.js project helps catch errors early and improves code maintainability. It offers even greater productivity and robustness to your web d
5 min read
How to use TypeScript to build Node.js API with Express ? TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will
4 min read
How to use the app object in Express ? In Node and Express, the app object has an important role as it is used to define routes and middleware, and handling HTTP requests and responses. In this article, we'll explore the various aspects of the `app` object and how it can be effectively used in Express applications. PrerequisitesNode.js a
3 min read