VS Code remote debug to NodeJs in cluster - node.js

I am trying to remotely debug NodeJs application, which uses clusters. When I run my app locally, and I am attaching to it, VS Code see child processes and breakpoints works:
VS Code Config:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 50131,
"internalConsoleOptions": "neverOpen",
"skipFiles": ["<node_internals>/**"],
"autoAttachChildProcesses": true
},
VS Code result:
However, in remote host, process starts same way, same port, I am able to attach debugger to it, but VS Code Can't see child processes and source maps (to connect local files breakpoints to remote, in debug config I have correct host address):
So the difference between local env and remote is Windows vs Unix, and in remote app is run via Forever (module, however, even Forever, without clusters remote debugging works correct).
Where can be an issue?
EDIT: I can't use SSH debug in my organization.

Give vscode extension Remote SSH a try. It let you run remote code locally.
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh

Related

VSCode debugger refuses to attach to node process with no error or logs?

We're using node v18.3.0
I'm starting my nestjs app, inside a docker container, with this command nest start -e \"node --inspect-brk\". Which gives the following output:
node#bfcdb32e737f:/opt/thallo/exchange-be$ npm run test:debug:wait
> exchange-be#0.0.1 test:debug:wait
> nest start -e "node --inspect-brk"
Debugger listening on ws://127.0.0.1:9229/c4d73a54-6165-4502-bcea-1fa4390db95c
For help, see: https://nodejs.org/en/docs/inspector
So it looks like it's waiting for a debugger to attach - excellent!
I have forwarded port 9229 on the docker container, docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfcdb32e737f exchange-be_exchange-be-uiapi "tail -f /dev/null" 7 minutes ago Up 7 minutes 0.0.0.0:3001->3001/tcp, :::3001->3001/tcp, 0.0.0.0:9229->9229/tcp, :::9229->9229/tcp exchange-be-uiapi
And I can telnet to that port from the host machine telnet localhost 9229:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
This is my launch.json:
{
// 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": [
{
"name": "Attach Exchange BE",
"port": 9229,
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "node",
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"restart": true
}
]
}
But when I launch the debugger I just get this little progress bar for a few seconds and then the debugger stops:
But there's no error log or anything so I have no idea how to fix this. Any help is greatly appreciated!
For debugging a NodeJS application running a docker 127.0.0.1 interface wont reach the docker network try using 0.0.0.0.
In the debug command at the package json change it to nest start -e \"node --inspect-brk=0.0.0.0:9229\"
The 0.0.0.0 will tell the debugger to look at all available local network interfaces.
In the vscode debug configuration "address": "0.0.0.0" and it is recommended to use "remoteRoot" and "localRoot" directives for syncing the position of the current vscode work directory files to the files in the docker file system.

How to attach remote source code to remote debugger in VS Code?

I have docker compose in a linux machine. The docker images are build out of node JS source code sitting in the same machine. My IDE is VS Code, but it is running on a different machine(Windows). I have configured Remote SSH so that the IDE in windows machine can browse through the source code in the linux machine using SSH connection. This setup is working fine for me.
But I have a need to do remote debugging of the app running in the docker. I am able to attach the debugger to the process, but when I add break point, it says 'Unbound breakpoint'. I think it is because the debugger is unable recognise the source code sitting in the remote machine.
The following is the launch.json config:
{
"type": "node",
"request": "attach",
"name": "Test",
"protocol": "auto",
"port": 4001,
"restart": true,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/src",
"skipFiles": [
"<node_internals>/**/*.js"
]
}
How can add a properly 'bound' break point?

How to debug "built" production NodeJS

I am using Visual Studio code to debug a node application in production environment
The Node process runs inside docker,
I port-forwarded and signaled USR1 to enable attaching debugger from VS code to that node process
My VS Code configuration is like this
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug: service",
"sourceMaps": true,
"smartStep": true,
"remoteRoot": "/src/",
"localRoot": "/home/my-username/work/orders/src/",
"protocol": "inspector",
"port": 9229,
"restart": true,
"address": "0.0.0.0",
"skipFiles": [
"<node_internals>/**",
"<node_modules>/**"
]
}
]
}
From VS code, I can hook into the application and the application can break on exception
However there is no source-mapping which cause all my breakpoint in my source-code to be "unbound breakpoint"
The loaded script list in VS code show that
The VS code debugger is able to see the node_modules and the built version of my source code inside dist. One other notable point is that the source code that is used to build the /dist is also available directly in the production server, in the upper folder.
How can I debug the built production process, using my unbuilt source code in this case?
I added Chrome behaviour as separate question
NodeJs: Chrome inspector can map source but unable to debug on original source
I don't know whether it will be helpful to you or not. But I think you have to use node-inspector. It can be used from any browser supporting WebSocket. It is really good.
Cool stuff
Node Inspector uses WebSockets, so no polling for breaks.
Remote debugging and debugging remote machine.
Live edit of running code, optionally persisting changes back to the file-system.
Set breakpoints in files that are not loaded into V8 yet - useful for debugging module loading/initialization.
Embeddable in other applications - see Embedding HOWTO for more details.
Node already ships with an integrated debugger. However, it doesn’t have a GUI, so you need to use the command line version.
You can launch this debugger using node debug.
$node debug test.js
< Debugger listening on port 5858
debug> . ok
break in test.js:1
> 1 var a= 5;
2 a = a*a
3 a += 2;
debug>
It shows you where it’s paused and then lets you control execution with commands like next and cont.
debug> next
break in test.js:2
1 var a= 5;
> 2 a = a*a
3 a += 2;
4
The repl and watch commands allow you to see the values of local variables.

How to run an Express app locally and connect to a remote Heroku Postgres instance

I have an Express.js app running in a Heroku instance and it connects just fine to the Heroku PostgreSQL instance I have connected to it when it is running in the Heroku app itself.
But when I try to run the Express app locally while developing and debugging, it cannot connect to the remote Postgres database.
This forces me to have to push small changes to the code handling database queries to Heroku and test them there, examine error messages through Heroku CLI, fix the errors locally, and repeat, which is significantly slower than if I was able to do this locally.
I have seen elsewhere that this is often due to SSL being disabled locally,
so I tried some solutions that people recommended like using Heroku CLI to port forward locally and then using Visual Studio Code to connect to this local version, but I was unable to get this working.
Below is the launch.json file I was told to use to connect to the database while debugging in Visual Studio Code:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Heroku",
"address": "localhost",
"port": 9229,
"protocol": "inspector",
"localRoot": "${workspaceFolder}\\source\\index.js",
"remoteRoot": "/app"
}
]
}
And below here is the command I was told to use to port forward the Heroku instance locally:
heroku ps:forward 9229 -a my-app-name
When I try running the command, it says it could not connect to the dyno and to check that its running. When I check, it shows that it is running, just idle, and that my quota for the month is nowhere near full.
Is there a step that I am missing? Or is there a completely different way of connecting to the remote database that is easier than this?

VScode debugger not stopping on breakpoints while debugging a remote node server

I'm trying to debug my remote node server through VSCode and it doesn't stop on any of the breakpoints i've configured.
I'm running VSCode on my mac where i have one copy of the node project and i have another copy of the same node project on a remote machine, both copies of the node project are synced.
i'm running the node server on the remote machine using the following command:
sudo node --inspect=5858 app.js
and my VSCode launch.json configurations are as follow:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "remote server addr",
"localRoot": "${workspaceFolder}",
"remoteRoot": "the path to project directory on remote server",
"protocol": "auto"
}
When i launch the debugger on VSCode it seems like it connects to the remote server but it doesn't stop at any of my breakpoints.
VSCode version is 1.28.1
Node version is 9.11.2
What am i missing?
I found the solution to my question the configuration i was missing is server=4771
when i run my app as follow:
sudo node --inspect=5858 app.js server=4771
VSCode debugger is able to debug my remote node server, more details about it are mentioned here: https://code.visualstudio.com/docs/extensions/example-debuggers

Resources