How to get VSCode to attach to nodemon started by NPM? - node.js

Can I have VSCode kick off my app using NPM run scripts and then attach to the resulting process to debug?
My project kicks off with nodemon via NPM script (to execute babel, etc). However, while the app kicks off, VSCode's debugger doesn't attach (breakpoints are skipped).
// from package.json
"scripts": { "debug": "nodemon --inspect --exec babel-node src/app.js" }
// from launch.json
{
"type": "node",
"request": "launch",
"name": "Debug",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"address": "localhost",
"port": 9229,
"protocol": "auto",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"autoAttachChildProcesses": true
}
It seems the debugger starts but breakpoints I placed next to a few test calls are ignored. Here's the terminal output:
[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node --inspect src/app.js`
Debugger listening on ws://127.0.0.1:9229/13ef6ca8-40da-4741-854a-467e4230b2a7
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Hey!
Waiting for the debugger to disconnect...
[nodemon] clean exit - waiting for changes before restart```

Looks like this was a fool's errand because it technically can't be done.

Related

while debugging in vscode changes in nestjs file does not take effect

I have a nestjs project in vscode and in debugging mode the changes in code does not take effect. Breakpoints hit normally, however, but for example a changed variable values are not to be seen - i.e. they stay as the previous ones (the ones before change).
My launch.json is the following:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"runtimeExecutable": "nodemon",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register", "--config", "nodemon-debug.json"],
"program": "${workspaceFolder}/src/main.ts",
"autoAttachChildProcesses": true,
"restart": true
}
]
}
And the nodemon-debug.json file has the following:
{
"watch": ["src"],
"ext": "ts, js",
"ignore": ["src/**/*.spec.ts"]
}
And strangely enough, when I make change in the code, the nodemon really registers it and restart the app, as can seen from the console:
C:\...\AppData\Roaming\npm\nodemon.cmd --nolazy -r ts-node/register --config nodemon-debug.json .\dist\main.js
[nodemon] 2.0.18
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] to restart at any time, enter `rs`
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] watching path(s): src\**\*
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] watching extensions: ts,js
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] starting `node --nolazy -r ts-node/register .\dist\main.js`
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[WEB] http://localhost:3000
Can anyone figure out why changes are not taking effect?
I finally found solution from this https://stackoverflow.com/a/63325135/7625979
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"start:debug",
"--",
"--inspect-brk"
],
"autoAttachChildProcesses": true,
"restart": true,
"sourceMaps": true,
"stopOnEntry": false,
"console": "integratedTerminal",
}
]
}
This solution indeed works for me perfectly and does not need either the vscode's setting: Debug > Javascript: Auto Attach Filter to "Always". Also console: integratedTerminal -option is handy because debug-info comes directly into the same console. Also, nodemon-debug.json is not now needed.
Script command in Package.json file are now:
"scripts": {
...
"start:debug": "nest start --debug --watch",
...
}
It seems that "--watch" is still needed here for the code changes to take effect.
I don't have a launch.json in my NestJS project in VSCode.
However, I did change the debug settings in VSCode to the following:
file -> preferences -> settings -> Debug > Javascript: Auto Attach Filter to "Always"
Then when I set a breakpoint my test stops on the breakpoint. If I make a change and save the file my test will restart automatically and stop on the breakpoint with the updated values.

Nodemon Inspect address already in use when refresh with vscode debugger

I am debugging a node application in Visual Studio, so I have to attach create a new configuration in launch.json:
{
"type": "node",
"request": "attach",
"name": "Debug API",
"remoteRoot": "/workspaces/telehealth/projects/api",
"localRoot": "${workspaceFolder}/projects/api",
"protocol": "inspector",
"port": 9229,
"restart": true,
"address": "localhost",
"skipFiles": ["<node_internals>/**"]
}
And the script in package.json is
nodemon -e js,gql --watch dist --watch typeDefs --delay 500ms --inspect=0.0.0.0:9229 ./dist/index.js
That works fine the first time, but when I save a file and the server restarts, it fails with the classical error address already in use because the debugger is running.
I have to turn off the debugger, wait 5 seconds and then save the file to restart the server. After that, I have to start the debugger again.
There should be a way to restart without stopping the running process to debug, but I couldn't find nothing

VS Code not hitting breakpoints for Node app running in Docker Container

Summary
I'm running a node app inside a docker container can't get the VS code debugger to hit breakpoints.
Docker Setup
The docker container exposes port 5859. Inside the container the node app is ran with this command:
nodemon -L --watch src --exec babel-node src/server.js -- --inspect=0.0.0.0:5859 --nolazy
It reports that the debugger is listening:
[nodemon] 1.19.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /app/src/**/*
[nodemon] starting `babel-node src/server.js --inspect=0.0.0.0:5859 --nolazy`
Debugger listening on ws://0.0.0.0:5859/5939f6b6-5ade-4ce5-9694-7df5f5b8385b
For help, see: https://nodejs.org/en/docs/inspector
VS Code Setup
And when I fire up the debug profile in VS Code it appears to attach. Below is line from the logs of the running docker container.
However, no breakpoints are hit when I set them. Is this a babel-node issue? Is there any suggested path forward to get node debugging to work with babel-node?
My VS Code debug config:
{
"type": "node",
"request": "attach",
"name": "Docker: GraphQL",
"port": 5859,
"protocol": "inspector",
"restart": true,
"remoteRoot": "/app",
"localRoot": "${workspaceFolder}"
}
I was not able to get this to work with nodemon, but modifying my .babelrc file to include inline source maps triggered VS code to hit the breakpoints I set. My .babelrc file looks like this:
{
"env": {
"production": {
"presets": [
["es2015", {"modules": false}],
"stage-1"
]
},
"development": {
"presets": [
["es2015"],
"stage-1"
],
"sourceMaps": "inline",
"retainLines": true
} }
}
And the corresponding script that docker calls in package.json. Port 5859 is exposed in the docker-compose file.
"start:docker": "babel-node src/server.js --inspect=0.0.0.0:5859 --nolazy",
For anyone else arriving via a search like me. In my case changing ${workspaceRoot} to ${workspaceFolder} in my vs code launch.json file worked to get a previously working project debuggable again.

Visual Studio code - cannot connect to runtime process timeout after 10000 ms

I was trying to launch the program from the debug console in VS Code but got the error on cannot connect to runtime process timeout after 10000 ms
launch.json
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"protocol": "inspector",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}"
},
{
"type": "node",
"request": "attach",
"protocol": "inspector",
"name": "Attach",
"port": 9229
},
{
"type": "node",
"request": "launch",
"port":9230,
"name": "Launch Program",
"program": "${workspaceFolder}\\bin\\www"
}
]
}
I am trying to debug with VS Code but got hit by the error as below. Am I configuring my launch.json correctly ?
A "launch"-type configuration doesn't need to specify a port. When you set the port parameter, it assumes that your launch config will include the --inspect parameter with that port.
If you have to specify the exact port for some reason, then you can include the --inspect parameter like:
{
"type": "node",
"request": "launch",
"port":9230,
"runtimeArgs": ["--inspect=9230"],
"name": "Launch Program",
"program": "${workspaceFolder}\\bin\\www"
}
But I recommend just removing "port" from your launch config.
I'm using nodemon and babel to start visual studio code and found that you need to make sure you have a configuration in package.json and launch.json that are compatible with visual studio code.
Really, that means that you need to find a configuration that allows you to launch your regular configuration from powershell as well as gitbash in windows. Here's what I came up with:
In package.json
"scripts": {
"start": "nodemon --inspect --exec babel-node -- index.js",
},
In launch.json
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch via Babel (works)",
"cwd": "${workspaceRoot}",
"port": 9229,
"program": "",
"runtimeExecutable": "npm",
"console": "integratedTerminal",
"runtimeArgs": [
"start"
]
}
]
}
When node starts you should see something like:
PS F:\noise\bookworm-api> cd 'F:\noise\bookworm-api'; & 'F:\applications\nodejs\npm.cmd' 'start'
> bookworm-api#1.0.0 start F:\noise\bookworm-api
> nodemon --inspect --exec babel-node -- index.js
[nodemon] 1.18.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node --inspect index.js`
Debugger listening on ws://127.0.0.1:9229/e6e1ee3c-9b55-462e-b6db-4cf67221245e
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
Running on localhost:3333
The thing you're really looking for is:
Debugger listening on ws://127.0.0.1:9229/e6e1ee3c-9b55-462e-b6db-4cf67221245e
This output shows that your debugger is waiting on a WebSockets request on port 9229. You communicate that to visual studio code with:
"port": 9229,
In your launch.json file.
If you don't see the port that the debugging server is waiting on then you probably need to add the --inspect flag to your start command in node.
I get this same error when I forget to close the browser from the last debug session. It holds on to the connection to the Angular proxy and prevents a new debug session from starting up. Once I close the browser, F5 starts up a new session without error.
duplicate Google Chrome shortcut → itest
press alt key doubleclick itest
itest Properties → Shortcut → Target :
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=6062 --user-data-dir="%appdata%\Google\Chrome\itest
VS Code, Menu Debug → Add Configuration → Chrome: Attach → "port": 6062, → Ctrl+Shift+D Debug → Switch Attach to Chrome → Start Debugging
Delete launch.json file and go to debug and create new launch.json file
Open Android Studio, Configure, ADV Manager, create or open an ADV.
In VS Code and in debug click emulate android cordova
Command line - cordova emulate android
{
"name": "cordova emulate android",
"type": "cordova",
"request": "launch",
"platform": "android",
"target": "emulator",
"port": 9222,
"sourceMaps": true,
"cwd": "${workspaceRoot}",
// "ionicLiveReload": true
},
I am using docker-compose to run a react express app. You can see the full compose file below.
I got the mentioned error, and I added a port mapping as follows to resolve the issue.
- "9229:9229"
Also the if you are using visual studio code, the address configuration in launch.json file inside of .vscode should be as follows.
"address": "0.0.0.0",
The full launch.json file is pasted below as well.
The full docker-compose.yml file looks as follows.
version: '3'
services:
proxy:
build:
context: ./proxy
ports:
- 7081:80
redis-server:
image: 'redis'
node-app:
restart: on-failure
build:
context: ./globoappserver
ports:
- "9080:8080"
- "9229:9229" ######### HERE IS THE FIX ##########
container_name: api-server
ui:
build:
context: ./globo-react-app-ui
environment:
- CHOKIDAR_USEPOLLING=true
ports:
- "7000:3000"
stdin_open: true
volumes:
- ./globo-react-app-ui:/usr/app
postgres:
image: postgres
volumes:
- postgres:/var/lib/postgresql/data
- ./init-database.sql:/docker-entrypoint-initdb.d/init-database.sql
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
volumes:
postgres:
Here is the launch.json file.
Take a look at this reference for this file settings.
{
// 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": "3",
"configurations": [
{
"name": "Docker: Attach to Node",
"type": "node",
"request": "attach", // The other option is launch
"port": 9229,
//"address": "localhost",
"address": "0.0.0.0",
//"localRoot": "D:\\Vivek\\MxWork\\Js\\GloboClientServer\\src\\globoappserver",
"localRoot": "D:/Vivek/MxWork/Js/GloboClientServer/src/globoappserver",
"remoteRoot": "/app",
"protocol": "inspector",
"skipFiles": [
//"<node_internals>/**",
"<node_internals>/**/*.js",
"${workspaceFolder}/**/node_modules/**/*.js"
]
//"program": "${workspaceFolder}\\src\\globoappserver\\index.js"
}
]
}
Go to Tools -> Options -> Debugging -> General
then disable
I'm using it successfully with the following options disabled.
In Visual Studio go to: Tools -> Options -> Debugging -> General
Enable JavaScript debugging for Asp.Net (Chrome, Edge, and IE)
Enable Legacy Chrome JavaScript debugger for ASP.NET.
In my case updating the core tools solved the issue.
Use the below command to update:
npm install -g azure-functions-core-tools

Run configurations - Cannot connect to runtime process

I'm atempting to run the node command npm run dev from the debugger in vscode.
My run config in launch.json:
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"dev"
],
"cwd": "${workspaceRoot}"
}]
My scripts in package.json:
"scripts": {
"dev": "npm run build:live",
"build:live": "nodemon --exec ./node_modules/.bin/ts-node -- ./app/*.ts"
}
But when i run the config i get this output:
npm --debug-brk=18538 run dev
> discordbot#1.0.0 dev /home/olian04/Documents/Projects/Node/JavaScript/DiscordBot.js
> npm run build:live
> discordbot#1.0.0 build:live /home/olian04/Documents/Projects/Node/JavaScript/DiscordBot.js
> nodemon --exec ./node_modules/.bin/ts-node -- ./app/*.ts
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `./node_modules/.bin/ts-node ./app/index.ts`
And then this error:
Cannot connect to runtime process (timeout after 10000 ms).
To me it looks as if it worked, but it still throws an error, and the code stops running when it does, why?
I ended up adding the "port" tag to the "configurations".
And the --debug-brk argument to the "build:live" node command.
Final configs:
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"dev"
],
"port": 5858,
"cwd": "${workspaceRoot}"
}]
Final script:
"scripts": {
"dev": "npm run build:live",
"build:live": "nodemon --debug-brk=5858 --exec ./node_modules/.bin/ts-node -- ./app/*.ts"
}

Resources