It can sometimes be convenient to remotely debug your Node.js for z/OS application from your local computer. One option is to leverage the
--inspect
option of Node.js, and connect Visual Studio Code to the Node.js inspector. We have created the following video which will demonstrate how you can do this:
https://mediacenter.ibm.com/media/1_vhcbme46In the video, we demonstrate how you can debug this simple Node.js application on your z/OS server:
// All process arguments.
let argv = process.argv;
// Remove name of program.
let cli = 1;
let args = argv.slice(cli);
// This should be the first CLI argument.
let name = args[0];
console.log(`Hello, ${name}!`);
The intention is for the application to print the message
Hello, World!
when executed with the command
node hello.js World
. However, there is a bug in the application causing it to instead output:
$ node hello.js World
Hello, /home/dpp/dpp-vscode-debug-demo/hello.js!
To debug this application, we must first start the Node.js inspector with the command
node --inspect-brk hello.js World
. If the inspector was started successfully, you should see a message similar to the following:
Debugger listening on ws://127.0.0.1:9229/718ef730-4684-470a-9145-654d9ee6f972
For help, see: https://nodejs.org/en/docs/inspector
This message tells us the inspector is now waiting for Visual Studio Code to connect on port 9229.
Please note that in our video demonstration, we used the address 0.0.0.0 for convenience. Address 0.0.0.0 allows any client to connect without restriction, so it is not recommended for production systems. If you are not using address 0.0.0.0, you must also set up a ssh tunnel by executing the following command on your local machine:
ssh -L 9221:localhost:9229 user@example.com
This starts a ssh tunnel session where a connection to port 9221 on your local machine will be forwarded to port 9229 on example.com. We forward to port 9229 as that is the port indicated in the message when the inspector was started.
For Visual Studio Code, we set up the following launch.json file:
{
// 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": "attach",
"name": "Debug",
"skipFiles": [
"<node_internals>/**"
],
"address": "localhost",
"port": 9221
}
]
}
If you are using a ssh tunnel, you should set the "address" field to "localhost", and the "port" field to 9221 instead of what is shown in the video.
Once everything is set up correctly, we can run the Debug configuration and a copy of the application code will open up automatically for debugging:
Once everything is set up correctly, you should see this debug interface.
In this interface, we are able to step through the code and make changes to variables in memory to see how it affects the execution. Keep in mind that code you see in Visual Studio Code is a read-only copy. Final changes to the code must be made on the server itself.
Congratulations, you are now able to remotely debug your Node.js for z/OS application from Visual Studio Code on your local computer. For further information on debugging Node.js applications, you can check out the
Node.js Debugging Guide or the
Node.js documentations.