Table of contents
1.
Introduction
2.
Prerequisites
3.
Getting Started
3.1.
Setting up Typescript
4.
Run the following command to confirm if the compiler is installed.
4.1.
Creating a Typescript config file
4.2.
Using Typescript with Node.js
4.3.
Compiling a TypeScript to JavaScript
5.
Frequently Asked Questions
6.
Key takeaways
Last Updated: Mar 27, 2024

Node.js with TypeScript

Author GAZAL ARORA
0 upvote

Introduction

TypeScript is a popular open-source language maintained by Microsoft. It is a javascript superset and is popular among developers these days.

 

TypeScript is a powerful tool that offers up a whole new world of possibilities for JavaScript projects. It makes the code more secure and robust by detecting many errors before it is even released - it detects issues as we write code.

 

Typescript supports modern coding styles, such as static typing and type checking. It is designed to create complex and high-level applications. It will be great to have TypeScript as the primary language to support Node.js functionality.

 

This lets you develop server-side applications with rigorous type checking, avoiding runtime type errors and other Typescript benefits while maximizing Node.js capabilities.

 

You will learn how to set up and run a Typescript application with Node.js. You will also run some Node.js packages within your Typescript application.

 

-Source

Prerequisites

 

  • Node.js installed on your computer. You can check it using:

 

node --version

         

        To generate the package.json file, run:

npm init
  • Basic understanding of Node.js.
  • Basic knowledge of TypeScript.

Getting Started

Setting up Typescript

 

You need to add Typescript dependencies to set up TypeScript with node.js. Run the below command to install the TypeScript Compiler Package. 

 

npm install -g typescript

Here, the above command will install the TypeScript compiler globally. This means that each project you create on your computer can use Typescript dependencies without reinstalling the Typescript package.

Run the following command to confirm if the compiler is installed.

tsc --version

 

 


Note: You cannot directly build Typescript code on a browser. A browser will only read JavaScript code. You'll need a compiler to convert TypeScript to browser-executable JavaScript before you can run any Typescript code.


 

 

Creating a Typescript config file

To start the Typescript project using Node.js, create a tsconfig.json file in the project root directory.

Run tsc --init to create this file in whatever directory you are in automatically.

 

The tsconfig.json should be:

{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"declaration": true,
"moduleResolution": "node",
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
}
}

 

Now type tsc in the terminal to compile; it will look for the tsconfig.json file. The target JavaScript will be es6, with the dist folder as the output directory and src as the input directory.

 

Feel free to add or modify this file as needed to configure the Typescript compiler according to your application's and environment's needs.

Using Typescript with Node.js

 

Create the src folder in your project directory. The Typescript files are stored under src. Create an index.ts file in src and begin writing Typescript code.

 

function sum (num1:number, num2:number){
    return num1 + num2;
}
console.log(sum(8,4))

 

Let’s execute the above Typescript using Node.js.

Compiling a TypeScript to JavaScript

Run the following command to compile your TypeScript file to JavaScript. This will build, compile and output JavaScript code into the path specified in "outDir": "./dist" of the config.json file.

 

tsc 

 

 Run the below command to execute the code.

node dist/index.js

 

 

It might be annoying to run the above commands every time you want to build and run code in a development environment. To make things easier, you'll need to install the ts-node package.

 


 

Note: Make sure you have Typescript installed in your local project before using Ts-node. Run npm install -D typescript to install it.

 


 

 

Now, configure your package.json script tag and start the build command as shown below.

"scripts": {
  "start""ts-node ./src/index.ts"
}

 

         You can now run npm start to execute the index.ts.

Also see, Difference Between Controller and Restcontroller

Frequently Asked Questions

 

Q1: Should I start with TypeScript or JavaScript?

 

Ans: You can’t learn TypeScript without first learning JavaScript. Because TypeScript and JavaScript have similar syntax and runtime behavior, anything you learn about JavaScript will also help you understand TypeScript.

 

Q2: Is TypeScript easy?

 

Ans: Honestly, learning TypeScript is more complicated than learning JavaScript because TypeScript extends upon JavaScript. So, firstly, you need to have a good understanding of how JavaScript works. But, with some practice and time, you should have no trouble learning.

 

Q3: What is node js used for?

 

Ans: It's a server-side programming language mainly used for non-blocking, event-driven servers like standard websites and back-end API services, although it was created with real-time, push-based architectures in mind. Every browser has its own JS engine and node.js.

Key takeaways

In this blog, we learned how to set up TypeScript with Node.js. By following these steps:

 

  1. Set up your development environment. To build applications using TypeScript, make sure you have Node. 
  2. Create the project structure.
  3. Configuring TypeScript. 
  4. Add ESLint
  5. Testing your application.
  6. Build a Docker image. 
  7. Debug using source maps.

 

Typescript will help in the development process, ensuring that you are free of bugs and minor errors. It detects errors during compilation and before the execution of the code. To learn how to use Typescript with NPM packages, click here.

 

This offers you a head start on identifying and correcting runtime issues. It will help you develop faster, especially if you're working on a large application.

 

Typescript has now become a popular language among JavaScript developers. Learn how to get started with TypeScript. Check this out. tutorial

 

Happy coding!

 

By-  Gazal Arora

 

Live masterclass