Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install TypeScript in Node.js

Install Node.js, add TypeScript globally or per project, then write, compile and run a file.

How-To Guides1 min read9 screenshots

Step 1: Install Node.js and npm

Download Node.js from nodejs.org, or install it with your package manager.

Installing Node.js

Check both versions:

node -v
npm -v
Node and npm versions

Step 2: Install TypeScript globally

npm install -g typescript
Installing TypeScript globally

Check it:

tsc --version
The TypeScript version

Step 3: Install TypeScript in a project

Local installs keep every project on its own version.

mkdir my-project && cd my-project
npm init -y
npm install typescript --save-dev
Installing TypeScript locally

Step 4: Configure TypeScript

npx tsc --init
Creating tsconfig.json

This creates tsconfig.json. Set "outDir": "./dist" and "rootDir": "./src" to keep sources and output apart.

Step 5: Write TypeScript code

Create src/index.ts:

function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("World"));
Writing a TypeScript file

Step 6: Compile

npx tsc
Compiling the TypeScript code

The compiled dist/index.js appears.

Step 7: Run

node dist/index.js
Running the compiled code

You see Hello, World!.

Quick recap

  • Install Node.js, then npm install typescript --save-dev.
  • Create tsconfig.json with npx tsc --init.
  • Compile with npx tsc, run with node.