../

Running TypeScript with Node.js

02 Mar 2025

Node.js doesn’t support TypeScript out of the box.

That doesn’t mean you can’t run TypeScript with Node. Just, that you need to do a little extra work.

Diagram

Let’s do that here!

Start with a new directory and cd into it.

mkdir my-ts-project
cd my-ts-project

Step 1 - Starting with a Node.js Project

Initialize a new Node.js project.

npm init -y

Step 2 - Installing TypeScript

To use tsc, we need to install TypeScript.

npm install typescript

This installs TypeScript locally in your project.

As we have installed TypeScript locally, we cannot use the tsc command directly from the bash as it doesn’t exist in the global context.

So, we can use npx (or pnpm dlx - for pnpm)

# Run this to initializes a TypeScript project
npx tsc --init

This creates a tsconfig.json file in your project. This file is used to configure TypeScript compiler.

Step 3 - Writing TypeScript Code

Create a new file index.ts

// index.ts
console.log('Hello TypeScript');

Step 4 - Running TS

To run this code, 2 steps are needed:

  1. Compile ts into js - using tsc
  2. Run js using nodejs
# This compiles all ts files in your project into js
npx tsc  

# OR

# This compiles all CHANGED ts files in your project into js (takes relatively less time)
# Avoids unnecessary recompilation of unchanged files.
npx tsc -b

The above command(whichever you choose to run) will create a new file index.js in the same directory.

Now, run the index.js file using Node.js

node index.js

And that’s it, you just ran TypeScript using Node.js