How to set up TypeScript with Node.js and Express (2023)
Sukanta Das

Sukanta Das @cristain

About: Software Engineer | Bangladesh 🌐💻 Diving into tech without a CSE background, yet my aspirations know no bounds. I'm rewriting my journey, determined to conquer every challenge and reach my goal.

Location:
Bangladesh , chattogram
Joined:
Nov 15, 2022

How to set up TypeScript with Node.js and Express (2023)

Publish Date: Jul 4 '23
109 13

In this article, we’ll cover a Best way to set up TypeScript in an Express app, understanding the basic constraints that come with it.

Table of contents

  • Create a package.json file
  • Installing TypeScript & other dependencies
  • Generating tsconfig.json
  • Create an Express server with a .ts extension
  • Watching file changes and build directory

1. Create initial folder and package.json

mkdir node-express-typescript
cd node-express-typescript
npm init --yes
Enter fullscreen mode Exit fullscreen mode

After initialize a package.json file , The newly created file might look something like the following code:

{
  "name": "Your File Name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js", // Entry Point change it from  js to .ts 
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

2. Installing TypeScript & other dependencies

npm install express mongoose cors mongodb dotenv
Enter fullscreen mode Exit fullscreen mode
npm install  -D typescript ts-node-dev @types/express @types/cors
Enter fullscreen mode Exit fullscreen mode

3. Generating tsconfig.json

npx tsc --init
Enter fullscreen mode Exit fullscreen mode

The command above will generate a new file called tsconfig.json with the following default compiler options:

target: es2016
module: commonjs
strict: true
esModuleInterop: true
skipLibCheck: true
forceConsistentCasingInFileNames: true
Enter fullscreen mode Exit fullscreen mode

After opening the tsconfig.json file, you’ll see a lot of other compiler options that are commented out. In tsconfig.json, compilerOptions is a mandatory field that needs to be specified

  • Set the rootDir and outDir as src and dist folder
{
  "compilerOptions": {
    "outDir": "./dist"

    // other options remain same
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Create an Express server with a .ts extension

Create a file name index.ts open it


import express, { Express, Request, Response , Application } from 'express';
import dotenv from 'dotenv';

//For env File 
dotenv.config();

const app: Application = express();
const port = process.env.PORT || 8000;

app.get('/', (req: Request, res: Response) => {
  res.send('Welcome to Express & TypeScript Server');
});

app.listen(port, () => {
  console.log(`Server is Fire at http://localhost:${port}`);
});

Enter fullscreen mode Exit fullscreen mode

5. Watching file changes and build directory

npm install  nodemon

Enter fullscreen mode Exit fullscreen mode

After installing these dev dependencies, update the scripts in the package.json file:

{

  "scripts": {
    "build": "npx tsc",
    "start": "node dist/index.js",
    "dev": "nodemon index.ts"
  }
}

Enter fullscreen mode Exit fullscreen mode

6. Run The Code

npm run dev 

Enter fullscreen mode Exit fullscreen mode

if everything if perfect you will see the message in console of
Server is Fire at http://localhost:8000

Comments 13 total

  • codefromrvk
    codefromrvkAug 11, 2023

    instead of nodemon in package.json it should have been ts-node-dev

    • Sukanta Das
      Sukanta DasAug 11, 2023

      yes this work can be done by ts-node-dev also

    • Timothy Brown
      Timothy BrownSep 19, 2023

      nodemon can run typescript files if you have the dependency ts-node installed.

  • talha
    talhaOct 21, 2023

    Not working showing success message of server is runnig. But when i try to request something request pn pedning...

    • Sukanta Das
      Sukanta DasNov 2, 2023

      I think there is some mistake on your side
      please recheck the code and try to set it up using the steps mentioned.

  • Muhammed Ajmal
    Muhammed AjmalDec 4, 2023

    everything will be fine..but even after running the server i can't find the dist folder

    • Sukanta Das
      Sukanta DasJan 1, 2024

      To see dist folder

      npm run build
      
      Enter fullscreen mode Exit fullscreen mode

      after running this you will see dist folder

  • Levi alkali damat
    Levi alkali damatJan 6, 2024

    work well for me. thanks

  • dev carnage
    dev carnageApr 19, 2024

    Everything is working. But in cases where I am not using typescript type properly, it's not giving any error while running server, any suggestions what I can use here?

  • Bryan Granado
    Bryan GranadoMay 5, 2024

    If is'nt work for you try this

    "scripts": {
    "build": "npx tsc",
    "start": "node dist/index.js",
    "dev": "ts-node-dev --respawn --transpile-only app.ts"
    },

  • Игорь Мешалкин
    Игорь МешалкинJun 20, 2024

    This is a great guide! Simple and useful. Thank you very much.

    • Sukanta Das
      Sukanta DasJul 1, 2024

      Thank you for your kind words! We're glad you found the guide helpful.

  • insanityAery
    insanityAeryAug 29, 2024

    "dev": " ts-node-dev --respawn --transpile-only src/server.ts"

Add comment