Last modified: Jul 20, 2025 By Alexander Williams
Install TypeScript in Node.js - Quick Guide
TypeScript is a powerful language that adds static typing to JavaScript. It helps catch errors early and improves code quality. This guide will show you how to install TypeScript in Node.js.
Prerequisites
Before installing TypeScript, ensure you have Node.js installed. You can check by running node -v
in your terminal. If not, download it from the official Node.js website.
node -v
You should see the Node.js version. If not, install Node.js first.
Install TypeScript Globally
To use TypeScript across all projects, install it globally. Run the following command in your terminal.
npm install -g typescript
This installs TypeScript globally on your system. You can verify the installation by checking the version.
tsc -v
You should see the TypeScript version. If not, check your npm installation.
Install TypeScript Locally
For project-specific installations, add TypeScript as a dev dependency. Navigate to your project folder and run.
npm install typescript --save-dev
This adds TypeScript to your devDependencies
in package.json
. It ensures TypeScript is only used for this project.
Initialize TypeScript in Your Project
After installation, initialize a TypeScript configuration file. Run the following command.
tsc --init
This creates a tsconfig.json
file. It contains compiler options for your TypeScript project.
Configure TypeScript
Open tsconfig.json
to customize settings. Here’s a basic example.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true
}
}
This configures TypeScript to compile to ES6, use CommonJS modules, and output files to a dist
folder.
Write a TypeScript Example
Create a src
folder and add a index.ts
file. Here’s a simple example.
// Define a function with TypeScript types
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("World"));
This function uses TypeScript’s static typing to ensure name
is a string.
Compile TypeScript to JavaScript
Run the TypeScript compiler to convert your code to JavaScript. Use this command.
tsc
This compiles all .ts
files in the src
folder to .js
files in the dist
folder.
Run Your TypeScript Code
After compilation, run the generated JavaScript file with Node.js.
node dist/index.js
You should see the output Hello, World!
in your terminal.
Automate Compilation
To avoid manually running tsc
, use the --watch
flag. It recompiles files on changes.
tsc --watch
This is useful during development. For more automation, consider tools like Nodemon.
Integrate with Other Tools
TypeScript works well with tools like Webpack and ESLint. These enhance your development workflow.
Conclusion
Installing TypeScript in Node.js is simple. It improves code quality and developer experience. Follow this guide to set it up for your projects.
For more Node.js guides, check our tutorials on React and Prettier.