Skip to main content

Command Palette

Search for a command to run...

Building a Node.js Backend with TypeScript: A Complete Guide

Updated
3 min read
Building a Node.js Backend with TypeScript: A Complete Guide
S

Hi, I'm Satyanarayan Dalei, a mid-level Full-stack web developer from India. Currently pursuing a master's in Computer Application, I've been coding since 2020. My expertise lies in the MERN stack, and I am well-versed in the software deployment life cycle, covering both production and development environments.

  1. Set up a new project directory

    1. Create a new directory for your project and navigate into it:
    mkdir my-node-server 
    cd my-node-server
  1. Initialize the project with Yarn:
    yarn init -y
    npm init -y // for npm user
  1. Install dependencies

    1. Install TypeScript and Node.js types:
    yarn add typescript @types/node --dev
  1. Install Express & it’s types
    yarn add express
    yarn add @types/express --dev
  1. Install ts-node & nodemon for executing typescript code & automatic restart on file changes change (After saving) respectively.
    yarn add nodemon ts-node --dev
  1. Set up TypeScript configuration

    1. Create a tsconfig.json file:
    yarn tsc --init
  1. In the tsconfig.json, add the following options by replacing all existing code.
    {
      "compilerOptions": {
        "target": "ES2019",
        "module": "commonjs",
        "outDir": "./dist",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true
      }
    }
  1. Write server code

    1. Create a src directory for your TypeScript files:
    mkdir src
  1. Create an server.ts file inside src and add a simple server code:
    import express, { Request, Response } from 'express';

    const app = express();
    const PORT = 3000;

    app.get('/', (req: Request, res: Response) => {
      res.send('Hello, World!');
    });

    app.listen(PORT, () => {
      console.log(`Server is running on http://localhost:${PORT}`);
    });
  1. Create .gitignore file

    In this step we will create a .gitignore file that is necessary using the following command. It will add content automatically based on language provided(node in this case).

     npx gitignore node
    
  2. Create dev, build and start scripts

    1. In package.json, add the following scripts:
    "scripts": {
        "build": "tsc",
        "start": "node dist/server.js",
        "dev": "nodemon --exec ts-node src/server.ts"
    },

Your package.json will look something like this after adding scripts

    {
      "name": "backend",
      "version": "1.0.0",
      "main": "index.js",
      "license": "MIT",
      "scripts": {
        "build": "tsc",
        "start": "node dist/server.js",
        "dev": "nodemon --exec ts-node src/server.ts"
      },
      "devDependencies": {
        "@types/express": "^5.0.0",
        "@types/node": "^22.7.4",
        "nodemon": "^3.1.7",
        "ts-node": "^10.9.2",
        "typescript": "^5.6.2"
      },
      "dependencies": {
        "express": "^4.21.0"
      }
    }
  1. Start development using following command

     yarn dev
    

    You should something like this in your terminal

     $ yarn dev
     yarn run v1.22.21
     $ nodemon --exec ts-node src/server.ts
     [nodemon] 3.1.7
     [nodemon] to restart at any time, enter `rs`
     [nodemon] watching path(s): *.*
     [nodemon] watching extensions: ts,json
     [nodemon] starting `ts-node src/server.ts`
     Server is running on http://localhost:3000
    
  2. Start building & serve backend app

    After successful development you can build & server your backend app.

    1. Build your app using following command
    yarn build

After build you will see something like this

    $ yarn build
    yarn run v1.22.21
    $ tsc
    Done in 2.60s.
  1. Now your application is built & you can start servers using following command
    yarn start

After starting server you will see something like below in your terminal

    $ yarn start
    yarn run v1.22.21
    $ node dist/server.js
    Server started