Cannot find Node process using VSCode Debugger - node.js

I'm testing out the VS Code node debugger, but I'm not able to find any node processes when trying to attach to a running process.
This is my launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}"
}
]
}
the contents of my package.json file:
"scripts": {
"start": "node --inspect=0.0.0.0:9229 ./bin/www"
}
After I start up the process using 'npm start', I press 'start debug' and the list of node processes is:
1 sssd_pam
1 sssd_nss
1 sssd_be
Looks like this and none of these are the server I just launched.
This list persists even after I take down the node server.
Why am I not able to see any of my running node processes in the VSCode process attach?
P.s. I'm closely following this tutorial on debugging Node.js with VS Code.

Are you running the NodeJS in debug mode inside npm start? You need to use the --inspect flag. Without this flag, the NodeJS interpreter won't open the debug port to the VSCode to attach to.
Refer to: https://nodejs.org/en/docs/guides/debugging-getting-started/
Another option is to attach using a port definition. I usually do something like this in the launch.json:
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"restart": true,
"sourceMaps": true,
"protocol": "inspector"
}
Then I start the NodeJS process as: node --inspect=0.0.0.0:9229 start.js

Related

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

How to debug nodeJS (ExpressJS) app in visual studio code?

I have tried many different solutions available online but nothing works in my case,
I am trying to debug a nodeJS app while its running, invoking the API through UI/postman
My launch.json: took guide from here
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"skipFiles": [
"<node_internals>/**"
]
}
After starting local server, when I start debugger, it ask to pick a process, I select the one ending with --exec babel-node server.js it attaches successfully but deosn't load my project scripts, only node_modules, eval and node_internal.
In my code if I put break point, I see this error "Break point set but not yet bound"
My package.json start sctipt:
"scripts": {
"start": "nodemon --exec babel-node server.js"
}
My code is in ES6, I start the server though a shell script which first set some environment then do npm start
My .bablerc
{
"presets": [
[
"#babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
Here is my launch.json and I run nodejs with express and it work fine.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"args": ["--env","local"]
}
]
}
Turns out I was picking wrong process ID.
When we start the debugger, it shows us the list of running processes to select with which debugger will be attached.
I picked the one ending with _babel-node server.js instead of --exec babel-node server.js. Doing this loaded all my script and started working as expected.

Alternative debug output for node/vscode, while building a terminal based visualization

i'm developing a terminal based app with Node, and i'm using the terminal as output of a Blessed.js visualization.
So, i can't use the terminal to console.log thing for debug.
Does node offer an alternative debug output? I'm using VSCode.
Thanks!
In package.json add in scripts section:
"scripts": {
"debug": "node --nolazy --inspect-brk=9229 main.js"
},
In vscode launch.json add:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229
}
]
}
Now:
First run blessed app with npm run debug
In 2nd step run vscode debugger with F5
You will see both blessed app interface and debugger in vscode.
Works for me ;)

What is the proper way to debug an npm script using vscode?

I've got an npm script that I'm trying to debug. I use vscode so I thought I'd create a debug configuration and step through it with the debugger.
My npm script look is:
"scripts": {
...
"dev": "node tasks/runner.js",
}
So I created the following debug config:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"runtimeExecutable": "npm",
"cwd": "${workspaceRoot}",
"runtimeArgs": [
"run", "dev"
],
"port": 5858,
"stopOnEntry": true
}
]
}
And when I fire it the script runs, but vscode is never able to connect and I get the error:
Cannot connect to runtime via 'legacy' protocol; consider using 'inspector' protocol (timeout after 10000 ms).
I tried adding an inspector protocol:
{
"type": "node",
"request": "attach",
"name": "Attach (Inspector Protocol)",
"port": 9229,
"protocol": "inspector"
}
And running the npm script via:
npm run dev --inspect
And this time I get the error:
Ensure Node was launched with --inspect. Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:9229).
I'm not sure what part I'm missing.
Edit per duplicate tag
I see the other question re: debugging an npm script via vscode, but the details in the other question and answers aren't as detailed and specific. If someone is searching for the specific vscode error messages I ran into or the config type I had they wouldn't necessarily get the level answer detail that this question's chosen answer gives.
You shouldn't try to debug the npm script because what you really want is to attach your debugger to the script that is launched with npm run command (NPM here is used only as a task runner).
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/tasks/runner.js"
}
]
}
If you really want to run it using npm script, then you can use the following configuration:
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"windows": {
"runtimeExecutable": "npm.cmd"
},
"runtimeArgs": [
"run-script",
"dev"
],
"port": 5858
}
but you also have to change your script command (specify a debug port)
"scripts": {
"dev": "node --nolazy --debug-brk=5858 tasks/runner.js"
},
You can explore various debug configurations simply by clicking the gear icon and selecting one.
More about Node.js debugging can be found in the VS Code documentation.

Can Visual Studio Code be configured to launch with nodemon

I have installed nodemon as a global package in my system.
It works when I executed nodemon in cmd.
But when I am using vscode with this launch.json file, vscode throws this exception:
request launch: runtime executable XXX\XXX\XXX\XXX\nodemon does not exists
the launch.json is:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "app.js",
"stopOnEntry": false,
"args": [],
"cwd": ".",
"runtimeExecutable": nodemon,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"preLaunchTask": "",
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
when I erase the nodemin in runtimeExecutable it runs perfectly with node
First, install nodemon as a dev dependency:
npm install --save-dev nodemon
For newer versions of VS Code set up your .vscode/launch.json file like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
"program": "${workspaceFolder}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}]
}
The most important pieces are the runtimeExecutable property that points to the nodemon script and the program property that points to your entry point script.
If you use an older VS Code (which you shouldn't), try this launch configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch with nodemon",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/nodemon/bin/nodemon.js",
"args": ["${workspaceRoot}/app.js"],
"runtimeArgs": ["--nolazy"]
}
]
}
The most important pieces are the program property that points to the nodemon script and the args property that points to your normal entry point script.
I couldn't get #AdrianT's answer working with the debugger attached. It seems like there's a newer built-in supported way to do this:
Open the Launch Configuration dropdown and select "Add configuration..."
Select "Node.js: Nodemon Setup"
It will add something like this to your launch.json:
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceRoot}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
Make sure your "program" setting is your correct entry point script.
You need to install nodemon globally to get this to work (npm install -g nodemon) (as per the documentation)
Your app now runs and you can set breakpoints which will be hit and the console logs to the integrated terminal window.
Note that terminating the debug session only terminates the program to debug, not nodemon itself. To terminate nodemon, press Control-C in the integrated terminal.
In Visual studio code create a launch config:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"restart": true
}
run nodemon from the command line: nodemon --debug server.js
Now 'Attach' from VC and vuala.
Attaching is definitely an easy option. In order to make sure that your code breaks, make sure you run nodemon with --inspect-brk (node 8+), e.g.:
nodemon --inspect-brk src/app.js
After launching nodemon will log the port open for debug connections:
Debugger listening on ws://127.0.0.1:9229/someUUID
You can take that port in order to build your launch config which is quite simple:
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"restart": true
},
I tried the solutions Adrian and Mathew suggested. They seem to work perfectly if your are on macOS (maybe also on Linux). On Windows, maybe Mathew's solution is more stable. Combining both to have a solution that could be compatible with both macOS, Windows and Linux, and makes sure that we don't face with errors like "PATH not found", I found that using globally installed nodemon for debugging seems to be much more stable.
Install nodemon globally (if you haven't done it before) npm i -g nodemon
Add the following to the .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug App_Name",
"skipFiles": [
"./path/of/file/to/skip/when/debugging"
],
"program": "app.js",
"restart": true,
"runtimeExecutable": "nodemon",
"console": "integratedTerminal"
}
]
}
We, of course, can still install nodemon locally and run it while developing.
What worked for me without global installs and using typescript:
{
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"name": "nodemon",
"program": "${workspaceFolder}/src/index.ts",
"request": "launch",
"restart": true,
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
"type": "pwa-node",
"args": ["--config", "nodemon.json"] //remove --config if you don't have one
}
In order to don't have problems with ts-node, in nodemon.json I added it:
{
"execMap": {
"ts": "npx ts-node"
}
}
No, currently it can't. But I managed to get this somewhat working using nodemon. I start it from Grunt . But an equivalent command line should do the same.
EDIT: After an evening of testing I can say that below approach is still somewhat flakey :S, attaching fails intermittedly and sometimes breakpoints are ignored.
EDIT2: You can also specify an non default debug port in Gruntfile using ['--debug-brk=5860'] for nodeArgs. I've been also advised to use --debug-brk instead of --debug. Perhaps this will remove the current flakeyness. I'll come back and mention here if it helps (I've currently switched project).
In case this might help anyone it's working with below settings in Current VS Code version (e.g. v0.10.6) on Windows 10. But it'll probably work on Mac too (I might check later). But note that I sometimes have to trigger a rebuild by changing+saving a file before the debugger picks it up.
/.vscode/launch.json
{
"configurations": [{
"name": "Launch",
"outDir": null
},{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}]
}
/Gruntfile.js
nodemon : {
dev : {
script : 'launcher.js'
},
options : {
ignore : ['node_modules/**', 'Gruntfile.js'],
nodeArgs: ['--debug'],
env : { PORT : '4123'
}
}
}
I guess the debug port 5858 is the default since it's not specified here (note it ís in launch.json above.)
https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
The above link helped me to successfully debug nodemon + express app. The steps are well explained there.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Node: Nodemon",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector",
}
]
}
npm script
"dev-server": "nodemon ***--inspect*** server.js"
Steps:
Run the server, using npm script. Please note --inspect arg in
the script
Start visual code debugger, A prompt will be shown to
select the node server process
select the node server process
Now you should be able to debug.
if it did not help you, then please have a look at the official doc, the config options are explained there.
https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools
This will let you run it on the file currently open in the editor WITHOUT installing nodemon as a dependency. This makes it convenient to keep in a template project.
The cwd and program are set so that the working directory is the one containing the file, and the program is the filename without a path. This makes it compatible with monorepos because it will then search back up the file tree for the correct tsconfig.json, package.json, node_modules, etc.
E.g. if the currently opened file is /path/to/some-file.ts, this is equivalent to running in the shell like:
cd /path/to
npx -y nodemon some-file.ts
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "npx",
"runtimeArgs": ["-y", "nodemon"],
"program": "${file}",
"cwd": "${fileDirname}",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Yes you can! As of a recent update you can attach the debugger to a running Nodemon process. This page has more information. Search for nodemon on the page to see the instructions.
I use the Node Exec plugin. It allows you to run and stop a node app in vcs by pressing F8 and F9 (applies on open file in editor). This could help as a (temporary) workaround.
Nodemon as local dependency
I also could not get #Adrian T's answer to work right away. But it is only a small detail, that has to be changed to make it work. So here we go:
create a launch.json file in a top-level .vscode folder
open the file in VS Code
use the build in button Add Configuration - that will be rendered in the editor - to add a config for Node.js: Nodemon Setup
in the generated config change the key runtimeExecutable:
"program": "${workspaceFolder}/app.js",
// diff from Adrian T
-- "runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
++ "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
You are now using the executables from your local dependencies (see this SO thread)
There is also an answer by Martin from earlier underneath this answer (just to be correct):
If you don't like having to run a global nodemon you can also install nodemon using npm and then set "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon", – Martin Feb 22 '18 at 22:25
Nodemon as global dependency
"runtimeExecutable": "nodemon", will only work if the executable of nodemon itself is part of the environment variable PATH (not `%appdata% or the like thereof). As this is mostly not the case, you would need to specify the absolute path (see the docs here and here).
For anyone trying to set up nodemon with an express-generator created ExpressJS project on windows, this is what worked for me when nodemon is installed as a local dev dependency (e.g. npm install nodemon --save-dev)
{
"type": "node",
"request": "launch",
"name": "Launch with Nodemon",
"runtimeExecutable": "node",
"runtimeArgs": ["${workspaceFolder}/node_modules/nodemon/bin/nodemon.js"],
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\bin\\www",
"env" : {
"DEBUG": "myapp:server"
}
}
No need to do anything,
Just open Powershell as administrator and write
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
and then enter A
1. Install nodemon as a dev dependency.
npm install -D -E nodemon
Options:
-D option: To save as dev dependency
-E option: To install exact version
2. Add a nodemon.json file in the root of the project with the following content.
{
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"execMap": {
"js": "node --harmony"
},
"watch": ["src/", ".env"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json",
"delay": 500
}
For more information, see the official documentation here.
3. Add a script to package.json.
npm pkg set scripts.dev="nodemon --config nodemon.json"
4. Add launch config in .vscode/launch.json.
Content:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Run Nodemon",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "dev"],
"envFile": "${workspaceFolder}/.env",
}
]
}
To see documentation here.
The envFile option is optional. I use it because I am using dotenv.
It works fine for me, in the main Terminal.
Do not use VS code terminal, use the main system terminal.

Resources