DEV Community

Uzeyr OZ
Uzeyr OZ

Posted on

TypeScript with Node.js and Express

TypeScript is a popular programming language that provides static typing and class-based object-oriented programming features. It is a superset of JavaScript and is compiled to plain JavaScript code. TypeScript provides many benefits, such as enhanced code readability, early error detection, and better maintainability of code. In this article, we will discuss how to set up TypeScript with Node.js and Express, which is a popular framework for building web applications.

Step 1: Install Node.js and npm

Before getting started with TypeScript, you need to have Node.js and npm (Node Package Manager) installed on your system. You can download and install them from the official Node.js website.

Step 2: Create a new Node.js project

Next, create a new Node.js project using the npm init command. This will create a package.json file, which is used to manage dependencies and scripts for your project.

Step 3: Install TypeScript

To install TypeScript, use the following command:

npm install --save-dev typescript

Enter fullscreen mode Exit fullscreen mode

This command will install TypeScript as a development dependency for your project.

Step 4: Configure TypeScript

Create a new file named tsconfig.json in the root directory of your project. This file is used to configure the TypeScript compiler.

Here is an example tsconfig.json file:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

Enter fullscreen mode Exit fullscreen mode

This configuration specifies that the target ECMAScript version is ES6, the module system is CommonJS, the output directory is dist, and the TypeScript compiler should enforce strict type checking. The include option specifies the files and directories that should be compiled, and the exclude option specifies the files and directories that should be excluded from compilation.

Step 5: Install Express and @types/express

To use Express with TypeScript, you need to install both the express package and the @types/express package, which provides TypeScript definitions for Express.

To install these packages, use the following command:

npm install express @types/express

Enter fullscreen mode Exit fullscreen mode

Step 6: Create an Express server in TypeScript

Now, let's create an Express server using TypeScript. Create a new file named app.ts in the src directory of your project. Here is an example app.ts file:

import express, { Request, Response } from 'express';

const app = express();

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

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

Enter fullscreen mode Exit fullscreen mode

This code imports the express package and the Request and Response types from the @types/express package. It creates a new Express application, defines a route for the root path, and starts the server on port 3000.

Step 7: Compile TypeScript

To compile the TypeScript code to JavaScript, use the following command:

npx tsc

Enter fullscreen mode Exit fullscreen mode

This command will compile the TypeScript files in the src directory and output the JavaScript files to the dist directory, as specified in the tsconfig.json file.

Step 8: Run the Express server

To run the Express server, use the following command:

node dist/app.js

Enter fullscreen mode Exit fullscreen mode

This command will start the Express server and listen for incoming requests on port 3000.

In this article, we have discussed how to set up TypeScript with Node.js and Express. We started by installing Node.js and npm, and then creating a new Node.js project.

We installed TypeScript as a development dependency, configured the TypeScript compiler using a tsconfig.json file, and installed Express and @types/express packages.

We then created an Express server using TypeScript and compiled the TypeScript code to JavaScript using the tsc command. Finally, we ran the Express server and tested it using a web browser.

By following these steps, you can easily set up TypeScript with Node.js and Express and start building web applications with improved code maintainability and error detection.

Top comments (0)