Debugging

The next big important question would how to debug a Node.js application return in TypeScript. Debugging JavaScript was easy, and to give the same experience TypeScript has a feature called sourcemaps. When sourcemaps are enabled in TypeScript, it allows us to drop breakpoints in TypeScript code, which will be paused when the equivalent line of JavaScript is hit. The sole purpose of sourcemaps is to map the generated source to the original source that generated it. We will briefly see debugging a Node.js, and TypeScript application in our editor VS Code.

Primarily, we need to enable sourcemaps. First of all, we need to ensure that TypeScript has sourcemap generation enabled. Go to your tsconfig.json file and write the following content:

"compilerOptions":{
"sourceMap": true
}

Now when you transpile your project, next to every JavaScript file you generate you will see a .js.map file.

The next thing is to configure VS Code for debugging. Go create a folder, .vscode, and add a file named launch.json. This is very similar to using node-inspector. We will debug the node-clusters project, which you can find in the source code. Open that project in VS Code; if it doesn't have a dist folder then generate a distribution by executing the tsc command at the main level, which will create the dist folder.

Next, create a folder named .vscode and inside it create a launch.json file with the following configurations:

VS Code debugging

When you click on Start Debugging, the following screen appears. Look at the screen, which has a detailed description of debugging points:

VS debugger