DEV Community

Cover image for Setting up a Node.js Express project with Typescript
Cedric Ahenkorah
Cedric Ahenkorah

Posted on

4

Setting up a Node.js Express project with Typescript

Setting up a Node.js project with TypeScript can significantly improve your developer experience by providing static type checking in your code. In this blog, I'll guide you through the steps to set up a Node.js Express project with TypeScript.

Create your project directory

Create a directory for your project and then navigate to it. In this example the name of our directory is node-ts.

mkdir node-ts
cd node-ts
Enter fullscreen mode Exit fullscreen mode

Initialize the project

Install a new node project with npm init. This will create a package.json file in the root of your project.

npm init
Enter fullscreen mode Exit fullscreen mode

Install TypeScript

Install TypeScript as a development dependancy.

npm install --save-dev typescript
Enter fullscreen mode Exit fullscreen mode

Initialize the TypeScript configuration file

npx tsc --init
Enter fullscreen mode Exit fullscreen mode

Set up your project structure

Create directories for your source (src) and compiled files (dist).

mkdir src
mkdir dist
Enter fullscreen mode Exit fullscreen mode

Install development tools

Install nodemon and ts-node as development dependencies to run and monitor your project.

npm install nodemon ts-node --save-dev
Enter fullscreen mode Exit fullscreen mode

Configure scripts

Configure a script in the package.json to run your server in development mode.

"scripts": {
    "dev": "nodemon --exec ts-node src/server.ts"
  }
Enter fullscreen mode Exit fullscreen mode

Configure nodemon

Create a nodemon.json file to configure nodemon to watch the TypeScript files.

{
  "watch": ["src"],
  "ext": "ts,json",
  "exec": "ts-node ./src/server.ts"
}
Enter fullscreen mode Exit fullscreen mode

Configure TypeScript

In the tsconfig.json, specify the compiler options and include your source files.

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "rootDir": "./src",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts"]
}
Enter fullscreen mode Exit fullscreen mode

Install Express

npm install express
npm install @types/express --save-dev
Enter fullscreen mode Exit fullscreen mode

Create a simple express server

Create a server.ts file in your src/ directory with the following basic set up for an express server.

import express from 'express';

const app = express();
const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send('Hello, TypeScript with Node.js and Express!');
});

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

Enter fullscreen mode Exit fullscreen mode

Start the development server

To start the server run the script you configured in your package.json file.

npm run dev
Enter fullscreen mode Exit fullscreen mode

Your Node.js Express with TypeScript project should be up and running now.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

The only thing worse than downtime? No Answers.

If you’re sometimes frustrated with opaque infrastructure, sluggish support, and mysterious outages, we prepared a webinar just for you

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️