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

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.
Set up a new project directory
- Create a new directory for your project and navigate into it:
mkdir my-node-server
cd my-node-server
- Initialize the project with Yarn:
yarn init -y
npm init -y // for npm user
Install dependencies
- Install TypeScript and Node.js types:
yarn add typescript @types/node --dev
- Install Express & it’s types
yarn add express
yarn add @types/express --dev
- Install
ts-node&nodemonfor executing typescript code & automatic restart on file changes change (After saving) respectively.
yarn add nodemon ts-node --dev
Set up TypeScript configuration
- Create a
tsconfig.jsonfile:
- Create a
yarn tsc --init
- 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
}
}
Write server code
- Create a
srcdirectory for your TypeScript files:
- Create a
mkdir src
- Create an
server.tsfile insidesrcand 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}`);
});
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 nodeCreate dev, build and start scripts
- In
package.json, add the following scripts:
- In
"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"
}
}
Start
developmentusing following commandyarn devYou 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:3000Start
building&servebackend appAfter successful development you can build & server your backend app.
- 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.
- 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



