Debugging Node.JS!

Log to console:

Node.js is a runtime environment for executing javascript outside browser. So, we don’t have window or document object in this environment.Fortunately console is now part of the global object of Node.js.

Throw Exceptions:

Throwing Exceptions to understand data or application flow is another way to debug an application. If you are shipping logs to a different application (log file, data dog etc), exception message can be seen there otherwise it can be found on console.

Debug with IDE:

Third way to debug is with breakpoints in your IDE. One of the popular IDEs for javascript is Visual Studio Code. We need to add a launch.json at the root of your Node.js application with the following configuration.

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/dist/index.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store