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

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

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?

Visual Studio 2019 node.js docker remote debug: breakpoint set but not yet bound

I'm trying to attach remote debugger of Visual Studio 2019 to node.js app in docker container. What I do:
I have created example node.js/express project in Visual Studio using "Basic Node.js Express 4 Application" template.
I have created docker image with following dockerfile:
FROM node:alpine
ADD ./TestApp/ExpressTestApp/ /
ENTRYPOINT node --inspect="0.0.0.0" /app.js
I launch container with following command:
docker run -p 3000:3000 -p 9229:9229 test
The app works, pages open on address http://localhost:3000/, console output states that debugger is active:
Debugger listening on ws://0.0.0.0:9229/73b5b00d-a492-4842-81e5-d50c12e64ac9
For help, see: https://nodejs.org/en/docs/inspector
In Visual Studio 2019, I click "Debug -> Attach to process...". In opened dialog I choose "Chrome devtools protocol websocket (no authentication)", enter ws://localhost:9229/73b5b00d-a492-4842-81e5-d50c12e64ac9 as a connection target and I can see and connect to node.js debugger in docker:
I can successfully attach to the process (node.js output states "Debugger attached."). But I can't use breakpoints in source files. When I place brakepoint, it shows the message "The breakpoint will not currently be hit. Breakpoint set but not yet bound."
How to make it work in Visual Studio?
I've tried to debug my app in VS Code, using following launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to remote",
"protocol": "inspector",
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/"
}
]
}
And it works perfectly. Breakpoints, variables values watching and etc. And I don't need to specify generated GUID of debugger, which is great too.
How to achieve the same thing in Visual Studio 2019?

VS Code remote debug to NodeJs in cluster

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

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?

Resources