---
title: "How To Get Started With TypeScript in Node.js | CoderPad"
description: "TypeScript was the third most popular programming language in 2022, following Rust and Python. It’s widely admired by the JavaScript community and used by many companies to build front-end and back-end applications. In this post, let's dive into TypeScript, learn how to get started with TypeScript in a Node.js applicat"
canonical: "https://coderpad.io/blog/development/how-to-get-started-with-typescript-in-node-js/"
last_modified: "2026-04-15 16:20:53"
---
[Back to blog](https://coderpad.io/blog/)

# How To Get Started With TypeScript in Node.js

March 3, 2023 6 Minute Read

By Siddhant Varma

[Development](https://coderpad.io/blog/development/)

TypeScript was the [third most popular](https://survey.stackoverflow.co/2022/#most-loved-dreaded-and-wanted-language-want) programming language in 2022, following Rust and Python. It’s widely admired by the JavaScript community and used by many companies to build [front-end](https://coderpad.io/blog/development/10-javascript-data-visualization-libs/) and back-end applications. Typescript’s versatility enables use with any framework or technology powered by JavaScript. This includes JavaScript back-end frameworks like Node.js, which developers use to build RESTful web services and back-end systems.

In this post, we’ll dive deeper into TypeScript and why it can be useful for you. Then you’ll learn how to use TypeScript in a Node.js application.

## What is TypeScript?

[Typescript](https://www.typescriptlang.org/docs/) is an open-source programming language developed by Microsoft. It’s an extended version of JavaScript that offers support for using variable types.

JavaScript itself is a weakly typed language. That means it assigns variable types at runtime instead of compile time. In Typescript, you can leverage the type system to create and declare type interfaces for your variables.

This kind of interface defines what type, either primitive or custom, your variable poses. Therefore we can say that TypeScript is a typed superset of JavaScript. When you compile your TypeScript code, the compiler translates it back to JavaScript. Therefore, you can also think of TypeScript as an abstraction layer for JavaScript applications, with an extended type system.

## TypeScript: pros and cons

As a weakly typed language, [JavaScript](https://coderpad.io/blog/development/what-you-never-learned-about-javascript-functions/) isn’t an ideal choice for writing applications that can grow in size and complexity. This is because variable types aren’t known beforehand, and there’s no development-time check to validate them. A string variable may be assigned a boolean at some point later that causes a bug in the program. [TypeScript](https://en.wikipedia.org/wiki/TypeScript#:~:text=Types.%5B25%5D-,Design,-%5Bedit%5D) prevents this by identifying those kinds of errors beforehand, due to its strictly typed nature.

TypeScript has been around for quite some time and is loved by the community. Adding support for variable types in your program not only makes it less prone to errors but also makes your code self-documenting. If any other developer looks at your TypeScript function and the variables it takes and returns, they can easily understand what’s happening in that code.

In addition, TypeScript has enhanced IDE support that automatically recommends types for your variables, function parameters, return values, and so on. Moreover, as a superset of JavaScript, you can always go back to writing a TypeScript function in pure JavaScript, if needed.

Therefore, TypeScript makes your application more robust, reduces errors, and makes your code self-documenting. However, as a strongly typed version of JavaScript, it has a steeper learning curve. Hence some developers find it difficult to work with, especially beginners.

## Getting started with TypeScript in Node.js

Now that you have an idea about what TypeScript is, let’s see how we can set up and start using it in a Node.js application.

### Setup

First, we’ll create a new directory for our Node.js project:

```
$ mkdir nodejs-typescript
```

Then, we’ll navigate inside this directory and create a new Node.js project:

```
$ cd nodejs-typescript && npm init -y
```

The above command should return an auto-generated`package.json`file as shown below:

A boilerplate`packgae.json`file generated from the`npm init -y`command.

Great! Now, let’s install TypeScript by running the following command in the root directory of the project:

```
$ npm i typescript -save-dev
```

I also like to install the`@types/node`module. This contains the type declarations for some common Node.js modules, like the file system and OS.

```
$ npm i -D @types/node
```

You’re all set! You should now have a`node_modules`folder in the project, along with a`package.json`file.

### Adding a TypeScript config file

Next, create a file called`tsconfig.json`in the root directory with the following contents:

```
{

    "compilerOptions": {

      "module": "NodeNext",

      "moduleResolution": "NodeNext",

      "target": "ES2020",

      "sourceMap": true,

      "outDir": "dist",

    },

    "include": ["*"],

  }Code language: JSON / JSON with Comments (json)
```

This file configures the behavior of the Typescript compiler. Let’s quickly go over what each compiler option means.

- The`module`option defines the module system for your application.
- The`target`specifies the version of JavaScript our code will compile to.
- The`sourceMap`option, set to true, compiles JavaScript code back to TypeScript for debugging, when needed.
- The`outDir`specifies the output directory where your compiled JavaScript code will reside.
- The`include`option tells Typescript where to look for the files to build.
- The`*`indicates that all our TypeScript code will live directly in the root directory.

### Building a TypeScript project

You have everything set up now. Let’s create our first script in Typescript and build it in JavaScript. Create a new file called`app.ts`in the root directory. Here, you’ll add some fairly simple and straightforward code to calculate the sum of two numbers:

```
const a: number = 10;

const b: number = 20;

function add(a: number, b: number): number {

    return a + b;

}

const sum=add(a,b);

console.log(`Sum of ${a} and ${b} is ${sum}`);Code language: TypeScript (typescript)
```

All Typescript files get a`.ts`extension. Notice how we declared types for the variables, function parameters, and return values. You’ve written your first Typescript code in Node.js — not bad!

Now let’s compile it. In your`package.json`file, under the`scripts`section, add the following script:

```
{
...

 "scripts":{

    "build": "tsc",

     ...

  }

}Code language: JSON / JSON with Comments (json)
```

Use the following command to execute the above script:

```
$ npm run build
```

It should look through your TypeScript files in the root directory and start compiling them:

The TypeScript build process.

Once that finishes, you should see a`dist`directory with the following files:

```
|- dist
  |- app.js
  |- app.js.map
|- node_modules
|- app.ts
|- package-lock.json
|- package.json
|- tsconfig.json
```

Here,`app.js.map`represents the source map code, whereas`app.js`file represents the compiled JavaScript code:

Compiled JavaScript code from TypeScript code.

You can now use this (`app.js`) JavaScript file in production wherever you like!

### Using ts-node

You’ve seen how to build and compile a TypeScript project to JavaScript for production, but what about development-time or run-time? You can run your Node.js app in dev mode and see any logs it creates in the terminal. You can do the same with TypeScript. For that, we need to use a package called`ts-node`.

It’s a just-in-time compiler for your TypeScript project that provides an execution engine for your TypeScript code. You can use it to run your TypeScript files locally at development time. You can either install`ts-node`in your project as a development dependency or install it globally:

```
$ npm i -g ts-node
```

Now you can run any Typescript file you want using the following command:

```
$ npx nodemon app.ts
```

We use`nodemon`here, which automatically watches for changes and runs the file again if it finds any:

The nodemon process running.

As you can see, the console output prints to the terminal. If you make any further changes to the`app.ts`file, the watcher should automatically restart the server.

### Is it OK to use ts-node in production?

You can use the ts-node development-time tool locally with Node.js to run your TypeScript application. You can also use it to debug your application in development mode.

But you don’t really need to use it for production because for your production application, you don’t need a just-in-time compiler for production. The only thing you need for a production Node.js TypeScript application is the compiled JavaScript version or the`dist`directory that you saw earlier.

However, if you still want to use`ts-node`in production, you can because there’s no performance overhead associated with it.

## TypeScript best practices

Some novice developers, as well as experienced developers, find TypeScript tricky to work with. Here are some of the best practices you can follow when working with TypeScript:

1. Use`let`and`const`wherever possible, instead of blindly using`var`for variable declarations. You should use`let`for local variable declarations. For variables that don’t mutate, use`const`.
2. To compare two variables, use`===`instead of`==`for a more fool-proof comparison that incorporates type checks in addition to the value.
3. Avoid using the`any`type for your variables as much as possible. The type`any`exists for complex use cases where it’s difficult to assert the type of a variable at the time of definition or declaration. Oftentimes, developers use it casually. This can make your code more prone to type errors and bugs in production.
4. Understand the difference between the`any`and the`unknown`type when you use them with your variables. You cannot use methods and properties on a variable with the type`unknown`. However, you can do so with a variable of type`any`.

## Conclusion

In my opinion, TypeScript is an awesome tool for writing back-end applications in Node.js. It may initially feel difficult to get the hang of, but once you do, you’ll never want to go back to JavaScript again. It gives you confidence that your code has no type errors and also helps other developers understand your code.

This post was written by Siddhant Varma. [Siddhant](https://www.linkedin.com/in/siddhantvarma99/) is a full stack JavaScript developer with expertise in frontend engineering. He’s worked with scaling multiple startups in India and has experience building products in the EdTech and healthcare industries. Siddhant has a passion for teaching and a knack for writing. He’s also taught programming to many graduates, helping them become better future developers.

## Subscribe for more CoderPad insights

## Trending Articles

Previous slide Next slide [Development

## What is Prompt Engineering?

Introducing AI tools such as ChatGPT and Bard helps increase the efficiency of tasks such as data generation, code generation and completion, text summarization, etc. How were these AI tools trained to answer almost every question you ask them? The answer, prompt engineering, is discussed in this blog post.

Read the post](https://coderpad.io/blog/development/what-is-prompt-engineering/) [Development

## A Guide To Using React’s useCallback() Hook

Are you looking to optimize your React applications? Have you heard of the useCallback hook? useCallback is a powerful React hook that helps you optimize your application by preventing unnecessary re-renders. In this post, we'll dive into React's useCallback() hook, define referential equality and callback functions and how to tie it all together.

Read the post](https://coderpad.io/blog/development/a-guide-to-using-reacts-usecallback-hook/) [Development

## MySQL vs. PostgreSQL: How Do They Compare?

A database is a convenient tool that allows users to access information, maintain records, and apply commonly used operations, such as insertion, modification, deletion, and data organization. Learn about MySQL vs PostgreSQL, their features, advantages, and disadvantages. Finally, check out which one is the best choice for you.

Read the post](https://coderpad.io/blog/development/mysql-vs-postgresql-how-do-they-compare/) [Development

## PostgreSQL LIKE Operator: A Detailed Guide

When using databases, searching for values that match a given pattern is a familiar necessity. Learn how you can retrieve entries with queries including a particular string using Postgres LIKE operator.

Read the post](https://coderpad.io/blog/development/postgresql-like-operator-a-detailed-guide/) [Development

## How To Use DataProvider In TestNG With Examples

When it comes to testing there are many tools and techniques available. In this post, we'll discuss how to use DataProvider with TestNG.

Read the post](https://coderpad.io/blog/development/how-to-use-dataprovider-in-testng-with-examples/) [Development

## How To Get Up And Running With Django Migrations: A Guide

Django migrations are a way of handling the application of changes to a database schema. By following the steps outlined in this guide, you can easily set up and use migrations in your Django projects.

Read the post](https://coderpad.io/blog/development/how-to-get-up-and-running-with-django-migrations-a-guide/) [Development

## Writing A Parameterized Test In JUnit With Examples

A JUnit parameterized test allows you to decouple data from structure, generating cleaner and more reliable tests. Learn more in this guide.

Read the post](https://coderpad.io/blog/development/writing-a-parameterized-test-in-junit-with-examples/) [Development

## How To Use Assert In JUnit With Examples

The JUnit Assert class is what you use to write unit testing assertions in JUnit. Learn more about it in this guide, full of examples.

Read the post](https://coderpad.io/blog/development/how-to-use-assert-in-junit-with-examples/) [Development

## A Simple Guide To TypeORM: What It Is And How To Use It

Considering current languages have object-oriented programming features, most database management systems are based on the relational model, with tables instead of objects. For these reasons, mapping relational objects is very important in development. In this article, you will learn what TypeORM, one of the main tools used today, is.

Read the post](https://coderpad.io/blog/development/a-simple-guide-to-typeorm-what-it-is-and-how-to-use-it/) [Development

## Using PreparedStatement in JDBC With Examples

In this post, we'll see why and how to work with JDBC and PreparedStatement. We will also learn along with examples using code.

Read the post](https://coderpad.io/blog/development/using-preparedstatement-in-jdbc-with-examples/) [Development

## React vs React Native: The Differences Explained

React and React Native are similar in that they both allow web developers to build applications based on the unique concept of components. But the two frameworks do this in radically different ways. In this post, we cover the key differences between react and react native frameworks, implementation, and which is an appropriate choice.

Read the post](https://coderpad.io/blog/development/react-vs-react-native-the-differences-explained/) [Development

## Logging in Django

The most challenging part of fixing a problem is identifying it. Unfortunately, this statement is exceptionally true when it comes to debugging. Learn how to use the power of logging into your Django applications to improve your debug process in this article.

Read the post](https://coderpad.io/blog/development/logging-in-django/)

## Hire with confidence.

See why engineering teams trust CoderPad to evaluate developers with realistic, AI-ready hiring workflows.

[Request a demo](https://coderpad.io/sales/)
