Setting up a Node.js Express project with Typescript

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("https://dev.to/", (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.



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.