How to create a Typescript express server in Nodejs

Atiq ur rehman
1 min readDec 14, 2022

First Run this command in Root directory of your project:

npm init

And then Install the necessary dependencies:

npm install express @types/express typescript ts-node nodemon --save

Create a tsconfig.json file in the root directory of your project and add the following configurations:

{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"rootDir": "./",
"sourceMap": true,
"outDir": "dist",
"esModuleInterop": true,
"strict": true
}
}

Create an server.ts file in the root directory of your project and add the following code to create an express server:

import express from "express";

const app = express();

app.get("/", (req, res) => {
res.send("Hello from Express server!");
});

const port = process.env.PORT || 5000;

app.listen(port, () => {
console.log(`Express server listening on port ${port}`);
});
  1. In the package.json file, add a start script to run the express server using nodemon:
"scripts": {
"start": "nodemon server.ts"
},
  1. To run the server in development mode, use the following command:
npm run start

Your express server is now created and running using typescript in nodejs.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Atiq ur rehman
Atiq ur rehman

No responses yet

Write a response