The IDE Visual Studio Code has a fully integrated debug that allows the developer to work proficiently with multi-languages support.

In this case we are interesting in debug a node.js application (javascript or typescript)

To debug a node.js app we have to choises

  • launch, the ide launch the application
  • attach, an external resource launch the application

Launch

Edit the “launch.json” vscode configuration file as follow.

If does not exist create it under .vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Test",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/test.js"
        }
    ]
}

In this case the main program of my node it’s called test.js. Modify it properly.

Then in the “Run and Debug” section, select the configuration (“launch test”) and press play.

Attach

Attach is my favorite method because it allows the developer to separate the IDE logic with the runtime environment.

You can use this configuration in a multiple situation

  • if you have a docker running a node.js app
  • if you would like to reach a node.js app that is running remotely
  • or just for local debug

Firstly you have to modify the package.json file of your application as follow.

In this case I added a new line called “start-debug” and I said to node to start with “inspect” properties and my node could be reachable from any client in my network.

If you would like that the standard behavior is in debug mode, then just edit the “start” line.

{
	"name": "test",
	"version": "1.0.0",
	"description": "",
	"main": "test.js",
	"scripts": {
		"start": "node test.js",
		"start-debug": "node --inspect=0.0.0.0 --trace-warnings test.js"
	},
	"author": "Alberto Ielpo",
	"license": "ISC",
	"dependencies": {
	}
}

Then reach your launch.json file (if you don’t have create it under .vscode/launch.json) as follow

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach Test",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "port": 9229
        }
    ]
}

Now start your application in the start-debug mode

NOTE:

  • if you are using a docker remember to expose the port 9229 and change the startup command inside the Dockerfile if needed.
  • If you would like to reach remote code ensure that the server allows incoming connections (firewall, port and so on)

A start example could be

08:47:29 alberto@alberto-5490:~/dev/git/ws-node $ npm run start-debug

> wsnode@1.0.0 start-debug /home/alberto/dev/git/ws-node
> node --inspect=0.0.0.0 --trace-warnings test.js

Debugger listening on ws://0.0.0.0:9229/8d7b84d5-87ff-4bbe-8bc1-ef4ff7644ab4
For help, see: https://nodejs.org/en/docs/inspector

Now finally you can reach the “run and debug” vscode tab , select your attach configuration (in this case “Attach Test”) and press play.

More info: https://code.visualstudio.com/docs/nodejs/nodejs-debugging

Categories: Js,Ts,Css

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.