You are currently viewing Using Typescript in NodeJS development

Using Typescript in NodeJS development

Recently, when starting out any new NodeJS project the first thing I install is typescript. This post covers how to set up your development environment with typescript.


Typescript is a static-typed version of Javascript. This means that you get all the features(and more) of Javascript but with type safety. This reduces the chances of runtime errors caused by trying to perform operations on a wrong variable type. These errors are caught during compilation and you can fix them before shipping your code. To learn more visit the typescript website.

To show how we can use typescript in a NodeJS project, we will be setting up a simple web server using KoaJS.

Step 1. Initialize project and install dependencies

Initialize the project

npm init

Install koa dependency

npm install koa

Install dev dependencies

npm install -D typescript @types/koa @types/node 

We install typescript and type definitions for koa and node as development dependencies because we only use them during development.

Step 2: Configure typescript

Initialize typescript config file

npx tsc --init
// message TS6071: Successfully created a tsconfig.json file.

This will create a default config file for you in tsconfig.json. This file will hold quite a number of options but I just want to highlight some key values

{
  ...
  "target": "es5",
  "module": "commonjs",
  ....
}

target specifies the target version of ECMAScript(Javascript). This is set to es5 by default.

module specifies how typescript should generate its compiled output. Here it is set to commonjs which nodeJS supports by default.  

For more information about the config fields, checkout the tsconfig section in the typescript docs.

Note that now that Node supports es6 modules by default, we can also change this to es6, telling typescript to compile the output as es6 modules.

Step 3: Create your server

Create a file index.ts with the following code

import Koa from 'koa';
const app = new Koa();

app.use(async (ctx) => {
  ctx.body = 'Hello World';
});

const port = 8080;
app.listen(port, () => {
  console.log('server listening at port %s', port);
});

Here we just create a new KoaJS server to listen on port 8080. Notice that the file extension is .ts for typescript. Typescript also gives us the opportunity to use the es6 import syntax for importing dependencies. 

Step 4: Run your server

Add build scripts.

In the scripts section of your package.json add

 "start": "tsc && node index.js"

This will compile your typescript code into commonjs Javascript, and start your server.

In your terminal, run

npm start

You should see the following output

> tsc && node index.js
server listening at port 8080

That’s it! You have successfully set up your Koa server with typescript.

Open a new terminal, and enter 

curl localhost:8080

You should see the “Hello World” response. You can also visit http://localhost:8080 in your browser.

Next, you can add more endpoints and start building out your app.

Be sure to check out KoaJs for more useful middlewares like router, cors, and bodyparser.

Have any questions, want to share your thoughts or just say Hi? I’m always excited to connect! Follow me on Twitter or LinkedIn for more insights and discussions. If you’ve found this valuable, please consider sharing it on your social media. Your support through shares and follows means a lot to me! 

Leave a Reply