How can I debug node in VSCode with sudo in mac? - node.js

I want to debug node like this in terminal:
$sudo node app
But, in vscode, I don't know where I can configure it.
This is my launch.json. Nothing has helped,
launch.json

In order to run the command as sudo you have to launch VS code under sudo. Here's how you do that ...
Install "code" as a shell command if you haven't already by opening visual studio code regularly then opening the command palette (press F1) and type "Shell Command". This will bring up "Install 'code' command in PATH". More on that here: https://code.visualstudio.com/Docs/editor/setup#_mac-os-x
Run "sudo code" in your terminal, and you should see that when you run "node app" it'll be running under sudo.

First, create attach process config:
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"port": 9222
}
and run it.
Second, run node to debug like this.
sudo node --inspect=9222 app.js

I have the same issue. I'm trying to debug an app that connects to port 443 on a Mac. In order to get around the problem I've been using I added the following to my package.json under scripts.
"debug": "sudo devtool server.js",
With this you can debug, just not as nice since you are using the chrome debugger verses the one in vscode. You will need to have devtool from chrome installed for this to work.

Related

How do I setup launch.json and tasks under Visual Studio Code to fully automate, connect to and debug a remote Python Flask code in Docker container?

There are at least 20 questions that are somewhat related to my question and have contributed to my knowledge of how to solve this problem, but none seem to cover the entire answer to my specific question. Also, it seems that the Microsoft documentation tells you how the parts function, but I have not found a comprehensive example of how to do all of this. I would like this question to cover it all if possible. I have a manual debug process which I will share below, but I want to further automate it with tasks and entries in the launch.json file so that I don't have to start anything manually. Would someone be willing to share their fully automated version of the exact steps in the automation with me?
This is the Configuration steps I followed to make Windows Visual Studio Code to work with the debugpy remote python
debugger. I have also added how to just use an interactive debug session using docker exec -it with pdb as well.
I have both detailed the files that were changed here and what the changes were so that you can understand how it
currently manually works.
Below I have two different ways of running the python debugger:
Run it from the pdb command line interactively from a terminal window:
Run it manually from the command line in a terminal window, but let Visual Studio Code connect to the debugpy program
and Docker container to do it interactively from Visual Studio Code.
The following procedures are adopted from the following 2 articles I found after reading the Visual Studio Code
documentation and after googling how to enable remote Docker Container Python debugging:
Article 1:
https://stackoverflow.com/questions/40233928/how-to-remote-debug-python-code-in-a-docker-container-with-vs-code#:~:text=In%20your%20Docker%20image%2C%20you%27ll%20need%20to%20make,something%20like%3A%20docker%20run%20-d%20-p%203000%3A3000%20my-image
Article 2:
https://code.visualstudio.com/docs/python/debugging#:~:text=Switch%20to%20the%20Run%20and%20Debug%20view%20%28Ctrl%2BShift%2BD%29%2C,which%20point%20you%20can%20use%20the%20debugger%20normally.
To allow the my.py code to use debugpy debugger used by Visual Studio, I added the following entries in the Docker file:
RUN /usr/bin/pip --no-cache-dir install -U debugpy
The procedure to run the pdb debugger manually and interactively from the Ubuntu 20.04 command line is as follows.
Only use this step or step 6 below but not both. This line runs the pdb debugger (not debugpy ) that Visual Studio uses.
At the Ubuntu 20.04 command line execute the following:
export USER=JOHN DIR=/data OUTPUTFILE=/data/output/output.txt
Here is how I run the python program using pdb passing in a call to the run_user module with 3 environment vars:
docker exec -it my_docker_container /usr/bin/python my.py run_user --my_user=$(USER) --my_dir=$(DIR) --my_file=$(OUTPUTFILE)
Now, to support doing debugpy using Visual Studio Code, I add the following 8888:3001 port to the
docker-compose.yml so that it looks as follows after the change:
webapp:
build: ./
image: my.webapp
container_name: my_docker_container
ports:
- 80:80
- 443:443
#----START OF docker-compose.yml changes
- 8888:3001
#----END OF docker-compose.yml changes
depends_on:
- database
Open the Ubuntu 20.04 command prompt
Procedure to run the debugpy remote module on port 3001 internally and 8888 on host machine so that the Visual Studio
debugger can be used. Note: you cannot run both the interactive pdb debugger of step 3 and this step together as it probably won't work and will hang up your machine for a reboot.
Here is how I run the python program manually using debugpy in Visual Studio
export USER="JOHN" DIR=/data OUTPUTFILE=/data/output/output.txt
docker exec -it my_docker_container /usr/bin/python -m debugpy --listen 0.0.0.0:3001 --wait-for-client my.py run_user --user=$(USER) --my_dir=$(DIR) --my_file=$(OUTPUTFILE)
I Add at least the following to the visual studio config in the ~/.vscode/launch.json as follows (.vscode/launch.json) in order to connect to the debugpy backend:
{
// 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": "Python: Remote Debug Docker JMK",
"type": "python",
"request": "attach",
"port" : 8888, // This is the localhost port that will be open on your local machine to talk to the debugger.:
"host" : "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/opt/webapp"
}
],
"logToFile": true
}
]
}
After saving the above changes, to the .vscode/launch.json file, you should be able to press Control+Shift+D and then you will have the debugger entry at the top of you screen with a green right arrow. Alternatively, you could press F5 to bring up the debugger window.
If you are not using Visual Studio code and using the interactive pdb debugger,
instead, after you have ran step 3, the debugger will attach and be paused at the first line of code.
You can then use pdb commands and step with next until you get to the next line of code
or you will have to set an initial breakpoint using import pdb pdb.set_trace().
Do not use import pdb or pdb.set_trace() within a Visual Studio Code
debugging session but attach using Visual Studio Code as described in the other steps as the two are
somewhat incompatible with each other because they were developed at different times in Python's history.
Here is where my exact specific question is asked. Not sure why it was flagged by your stupid bot
What is my exact question to the community and how is it different from other questions?
I am not interested in doing the manual steps at step 3 to run either the pdb debugger, nor the debugpy (at step 6) and would like a fully working example of how to set up the tasks and launch.json entries to automatically run the manual steps at step3 and Step 6 within Visual Studio Code.

VScode python extension does not take changes into account

I am writing a simple script in Python using VS Code. I have the python extension installed and I use a conda environment.
To run my script, I use Shift+Enter (Run Selection in Python Terminal). This works.
However, if I change something in the script and do the same, the change is not taken into account. I am forced to delete the terminal and relaunch again.
Is this normal? How do you guys go at it?
The "Run Selection/Line in Python Terminal" option basically opens a Python console and copies the selected lines to it (as if you typed it yourself). When you make changes to your code and repeat the process, it copies those codes to the same console session, so whatever was copied before, still exists and will be used for next runs. You have to make sure to always run the same set of lines or, as what you are doing now, restart the Python console.
Instead of the "Run Selection/Line in Python Terminal" option, I recommend creating a launch configuration for your script, and then using the debugger.
Let's say you have this workspace:
|- myscript.py
|- .vscode
Start by opening your Python file on the editor, then opening the command palette (Ctrl+Shift+P or CMD+Shift+P), then calling Debug: Open launch.json.
Select Python File for simple scripts. That will automatically create a launch.json file under the .vscode folder, with default launch configurations for Python files. Modify it as needed for running your script. For ex., I prefer setting internalConsole for the console.
{
// 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": "run-myscript",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/myscript.py",
"console": "internalConsole",
"args": [],
"env": {}
}
]
}
Then from the Debug panel, select the launch configuration from the dropdown (same name as the "name" you set in launch.json), then hit the Run button.
Check the output from the Debug Console:
One good thing about this setup is that you can set breakpoints in your code, and then go through your code line-by-line, which is quite useful for debugging.
For more info, see Python debug configurations in Visual Studio Code.

How to access a running nodemon script after closing and reopening the terminal?

I'm running a node.js script using nodemon, but I'm wondering if it's possible to reaccess the nodemon process using the terminal after closing it.
Normally I would type "npm run start" that points to a "nodemon index.js" in the package.json, and some yellow and green logs would appear to tell the state of the process(starting, restarting). But if I close the terminal, how can I access this process again, without having to type another "npm run start"?
Just the case nodemon is not suitable for that, I would like to know which tools could help me achieve that, such as pm2, etc.
Open a screen or tmux session, and then run your app.
This permits the terminal to be disconnected from the program and reconnected later by reviving the same session.
cntr+j leaves your server running in the background, then if you work on the code and saves it and the server crashes because the server is still running.
A solution is just to minimize the terminal and not to close it, or opening via command prompt.

Visual Studio Code to use node version specified by NVM

Is it possible for VS Code to use node version specified by NVM?
I have 6.9.2 installed locally. Even after switching to another version, from the OS X terminal (not the VS Code terminal), restarting VS Code, VS Code still shows using 6.9.2.
OS X terminal
MacBook-Pro-3:~ mac$ node -v
v7.8.0
VS Code Terminal
MacBook-Pro-3:QB-Invoice-API mac$ node -v
v6.9.2
In VS Code:
go to your launch.json file
add the runtimeVersion attribute inside configurations as shown below
In this example, we are assuming 4.8.7 is already installed using nvm:
{
"version": "<some-version>",
"configurations": [
{
"type": "node",
"runtimeVersion": "4.8.7", // If i need to run node 4.8.7
"request": "launch",
"name": "Launch",
"program": "${workspaceFolder}/sample.js"
}
]}
The solution is to set alias default. In the OS terminal run -
nvm alias default 7.8.0
Open vscode, now running node -v returns 7.8.0
It seems vscode takes up this (alias default) value and not the node version that is set by nvm use X.X.X
Restart VS code for it to pick up the changes.
add runtimeExecutable to your .vscode/launch.json like this
{
"type": "node",
"request": "launch",
"name": "App",
"program": "${workspaceRoot}/index.js",
"runtimeExecutable": "${env:HOME}/.nvm/versions/node/v6.9.2/bin/node"
}
I had the same problem of being unable to keep my node version specified trough nvm in my OS X environment not only with VSCode but also with Atom Editor (using the platformio-ide-terminal package for managing the integrated terminal in it). None of the suggestions in the previous answers worked for me, besides me not using the debugger but using gulp and grunt for specific tasks. Apparently nvm does not get along with the integrated terminals or sub shells at least in these editors because when loading them the environment variable $PATH is modified internally and does the following according to a comment by one of the contributors of this package in this issue reported here NVM fails to load within nested shell #1652:
" #charsleysa I know why nvm is throwing this error. In your subshell, somehow the /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin part of your PATH has been moved from the end of the PATH to the start.
When nvm is then started, it calls nvm_change_path (my contribution changed it to this from nvm_prepend_path), which modifies the nvm-relevant part of the path in place.
Nvm then checks the current npm prefix by asking npm what it is. Since /usr/local/bin/npm now has precendence, it reports /usr/local/bin.
Nvm then checks whether the current prefix as reported by npm is in the directory tree of the current nvm node version (at this stage, the installation directory of the node version your default nvm alias resolves to).
The prefix is not part of that tree, so it deactivates itself (calling nvm_strip_path in the process, which is why there's no nvm-related path in your subshell's PATH), and bails with the error you're getting.
macOS's /etc/profile (or /etc/zprofile) calls /usr/libexec/path_helper, which does the PATH switcheroo.
In the parent shell, the PATH doesn't yet have an nvm dir in it, so by the time nvm runs, it prepends its directory to the path. But in the subshell, PATH has been reconfigured by macOS to put any non-system directories at the end and we have the problem."
I was always getting this message when launching any integrated terminal:
nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local"
Run npm config delete prefix or nvm use --delete-prefix vx.x.x --silent to unset it.
What I did to solve this in my case was the "workaround" part of that same issue reported which is essentially the following:
Reset the path by adding the following line inside my ~/.bash_profile at the very top before anything else:
PATH="/usr/local/bin:$(getconf PATH)"
And after that no more warnings when I launch any integrated terminal on both editors and I can interact with nvm to switch between any node version easily and without problems at all.
Here it is another alternative just in case this one doesn`t help that much.
Some of the answers provided are correct and upvoted, but somewhat incomplete. This procedure worked for me:
Open the terminal window inside VS Code and run node -v. You'll get for instance v10.12.0.
Open a terminal window outside VS Code Change your node version with nvm (ie. nvm use v12.14.0)
Cmd+ Shift + p and choose Preferences > Open Settings (JSON)
Add "terminal.integrated.shellArgs.osx": [] to your user config
Cmd+ Shift + p and choose Shell Command: Install 'code' command in PATH
Close VS Code.
Open a terminal window and run code. This will open VS Code with a new and updated bash / zsh session.
Open the terminal window inside VS Code and run node -v. You'll get v12.14.0.
Bonus: If you always want to get a particular node version on VS Code's terminal, set it as default by opening a terminal window outside VS Code and running:
nvm alias default v12.14.0
I am using oh-my-zsh and it too was not using the node version specified by nvm. Tried several suggestions posted here, but the only way I managed to resolve this issue was by adding the following line to the top of ~/.zshrc
PATH="/usr/local/bin:$(getconf PATH)"
I had the same problem, but the above answers didn't help.
Apparently the default shellArgs for osx are set to bash while I'm using zsh. I solved the problem by setting the shellArgs in my user settings to an empty array:
"terminal.integrated.shellArgs.osx": []
A alternative solution I've found is to simply launch code from the shell after you pick your node using nvm.
You need to first open the command pallet and select "install 'code' into
path".
And then launch a terminal and select your node via nvm and then launch "code".
I had this same issue and I found a strange workaround that may be helpful to someone else in the future.
If I do not set eslint.runtime my system was running node v10.11.0 for eslint server, whereas I wanted it to be running v12.13.0 which I had installed and made default via nvm.
I found that the v10 version of node was installed by brew based on #franziga's answer but my desired version of node was installed by nvm. So, I uninstalled v10.11.0 via brew and closed/reopened VS Code. Strangely, eslint was still reporting that it was started using v10.
I tried running a shell without any changes to my PATH in any startup scripts, and the version of node was still correctly pointed to v12 as expected, but VS code still starts up v10 for eslint.
I'm not sure how to check the path of the executable that is being run by eslint, and if I open an integrated terminal everything works fine with the expected version of node (v12).
Solution (for me):
I found that if I set "eslint.runtime": "node" in settings.json that it will now use whatever version of node was active when I opened vscode using code . on the terminal. Just "node" - no path.
Lots of complicated answers here. In my case, this was caused by node having previously having been installed. Fixed it by deleting the following directories: rm -rf /usr/local/bin/npm and rm -rf /usr/local/bin/node, then running nvm use default in VSC to pick up the node version installed by nvm.
Particularly with the shell I had no problems, but you may:
check your shell is properly configure or change it (you might be using different shells for vscode or your terminal)
check your env and if it's not properly set use the terminal.integrated.env.<platform>
I had issues with vscode itself and no solution could help me. So I finished using the following launch script.
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/server.js",
"runtimeExecutable": "/bin/bash",
"runtimeArgs": ["-c", ". ~/.nvm/nvm.sh;nvm run default \"$#\"", "dummy"]
},
this assumes you have it configure for bash (otherwise change it to your shell) and you want to use the default node version as configured by nvm (you may also change it).
Note: The "dummy" parameter is required so the rest of the parameters are properly parsed.
A longer explanation of "dummy": Shell scripts use positional parameters where the first one will be the script location itself (addressed by $0), when using the -c flag the script is read inplace an there is no $0 being set. vscode will pass some arguments, like the node start script location which will be wrongly interpreted, so "dummy" pushes all parameters one spot. It can be just anything, but it must be there.
Setting the default alias only worked for me after closing all instances of VS Code. Simply reloading the window wouldn't work. nvm ls would show the default alias set correctly but would keep using the version set when VS code was first opened (across all windows).
There's also the issue of having multiple node versions across repos, which is really what nvm is meant to solve. In the end I added the following to the bottom of my .zshrc file:
[ -s "./.nvmrc" ] && nvm use
Essentially when a new shell starts, it checks to see if a non-empty .nvmrc exists in the current directory and if so, uses that version. If that version is not installed you will get a message saying so. After running nvm install it should load properly in all new terminal sessions.
You can also use the automatic version switching on directory changes shown in the nvm README as #asiera pointed out. Although a project terminal will typically always open in the same directory as your .nvmrc file, so this solution is a bit simpler and only runs on terminal startup.
I found that setting the node version locally in a sub shell before calling code works well, while not changing the version in the current shell, e.g.
$ (nvm use 14; code .)
Therefore, to make it work transparently for any project folder, create a file .precode in the project folder with shell commands to source before starting code - e.g.,
nvm use 14
Then add to ~/.bashrc
pre_code(){
if [ $# == 1 ] && [ -f ${1}/.precode ] ; then
echo "(source ${1}/.precode ; `which code` ${#})"
(source ${1}/.precode ; `which code` ${#})
else
`which code` ${#}
fi
}
alias code='pre_code'
(Note: Run source ~/.bashrc in any shell opened before the edit in order for the edit to take effect.)
Then, assuming the necessary file ~/myproject/.precode exists, starting code with
$ code ~/myproject
will result in some diagnostic output on the shell, e.g.
source github/myproject/.precode
Now using node v14.15.1 (npm v6.14.8)
as well launching a new vscode window with the correct node version in the terminal window and debugger. However, in the shell from which it was launched, the original node version remains, e.g.
$ node -v
v12.19.1
I tried all the suggested solutions but nothing was working.
/usr/local/bin/node was pointing to somewhere. i made a symlink to a specific nvm node folder and that was solving the issue for me:
ln -s /Users/mad/.nvm/versions/node/v11.1.0/bin/node /usr/local/bin/node
I have the same problem and I found that I have node installed by brew and nvm. I uninstalled node installed by brew and the versions on both terminal and visual studio code are the same now.
You don't need to modify your default node version. The following example assumes that node 6 is your default version and you want VSCode to reference version 7 of node:
# open a terminal instance
nvm use 7
code . # or project folder instead of "."
# when VSCode start, you may use ctrl+` to open the integrated terminal
# then check the node version in the integrated terminal
node -v # should print 7
After reading this thread and testing almost all suggestions, I found a very simple solution if you are using nvm: Add nvm use in the command.
It's gonna take a little more time to start the debugger, but it is worth it to me because now I don't have to execute nvm use and open Vscode by the terminal every time I start working on a different project.
Here is my .vscode/launch.json file. Good luck!
{
// 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": [
{
"command": "nvm use && yarn start",
"name": "Launch",
"request": "launch",
"type": "node-terminal",
},
]
}
I wanted the solution to be workspace specific and not requiring any action on my part (not having to redo nvm use <version> each time i start a terminal)
The solution I found:
create the .nvmrc file at the root of my project that old the node version I want to use as stated in nvm ReadMe
adding the automatic activation script in my ~/.zshrc also in the ReadMe (bashrc script also in the readme)
So, your nvm is configured well, but other version of node STILL keeps taking over?
Remove all non-nvm versions of node:
brew uninstall --force node (yarn will be fine without system node)
Other version installed from pkg or other non-nvm method
Re-login. Now, nothing can be fighting for path with nvm no matter how is shell launched.
Note: When installing/upgrading yarn, use brew install yarn --without-node
VSCode Shell args seems to be deprecated, here's update using profiles in VS Code's settings.json:
This gets rid of the -l terminal argument turning it into an interactive shell vs a login shell.
"terminal.integrated.profiles.osx": {
"zsh (normal - me)": {
"path": "zsh",
"args": []
}
},
"terminal.integrated.defaultProfile.osx": "zsh (normal - me)"
Thanks! the answers here for explanation and here for old args way:
For me I simply did:
# in a separate terminal (i.e not in VScode teminal)
nvm install <node version>
then in VScode terminal:
nvm use <the node version you installed>
In case you'd like to set the Node version for your Visual Studio Code NPM script runner, here's what works on a per-project basis. So, without having to set global nvm defaults.
By "NPM script runner" I mean the hover-and-execute-scripts functionality directly in package.json:
Step-by-step
Place an .nvmrc file containing the project's Node version into the root folder of your project.
Enable automatic environment as described here: https://github.com/nvm-sh/nvm#deeper-shell-integration.
Open VS Code's settings.json and define your preferred shell (in my case, it's zsh). For the automation profile, it's important to define a login and interactive shell (arguments -l and -i):
"terminal.integrated.automationProfile.osx": {
"path": "/bin/zsh",
"icon": "play",
"args": ["-l", "-i"],
},
"terminal.integrated.profiles.osx": {
"bash": null,
"zsh": {
"path": "/bin/zsh",
"icon": "star",
}
},
Result
Opening a new shell triggers NVM (The icons show which setting is working):
And running an NPM script triggers NVM:
Cheers!
following solution worked for me
first install and use the desired node version with nvm with these commands: nvm install v16.13.1 and nvm use v16.13.1.
then get the pathname of the currently using node with which node command on Linux.
it will be something like this /usr/local/nvm/versions/node/v16.13.1/bin/node
finally use this pathname in launch.json for runtimeExecutable option.
the launch.json file
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
--> "runtimeExecutable": "/usr/local/nvm/versions/node/v16.13.1/bin/node",
"request": "launch",
"args": ["testcode/hunter.js","127.0.0.1:9229"],
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js"
}
]
}
If none of this answers worked for you,
If you have previously installed node by downloading and unzipping it.
Go to usr/local/lib and there will be this guy sitting around named nodejs.
Kick him out.
And set nvm alias default to preferred version again.
That's it, happily ever after. At least worked for me though.
According to the docs of nvm, you need to add this code snippet to your ~/.bashrc, ~/.profile, or ~/.zshrc file, so open the file and paste this in, restart vscode and enjoy nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
source: https://github.com/nvm-sh/nvm#manual-install
Did not tried all of the solution, but for me updating nvm simply worked.
Just follow the installation here and make sure that you bash_profile is updated.
None of the other solutions worked for me.
So I ran nvm alias default node and this fixed it for me.
I tried several of the options here at the top and they didn't work. I found a simple solution. In the VS Code terminal:
Click the down arrow on the terminal dropdown
Select Default Shell
Select 'bash'
Try node -v and that should return the correct version that was set as default nvm alias default v12.14.0
Check your default interactive shell on your MAC.
If it's zsh, how about setting the terminal in VS Code to zsh mode as well? Then you can use the specified node version on the Mac. This works for me.
I'm using macOS Big Sur v11.2.1 + VS Code v1.55.1
Setting pictrue
sudo rm -rf /usr/local/opt/node#<YOUR_NODE_VERSION>
then restart the Visual Code

Cannot find runtime 'node' on PATH - Visual Studio Code and Node.js

With a downloaded and installed version of Visual Studio Code 1.2.1, and a 64bit version of node.exe msi placed in my working directory (I am assuming that is correct), how do we add node and npm command line tools to be on our PATH? I am confused in understanding that statement. Where and how do we implement that? I am quoting this requirement directly from the top of this resource page - https://code.visualstudio.com/Docs/runtimes/nodejs
As a result of my current situation, I set a break-point in an app.js file. And when I hit F5, it tells me...
Cannot find runtime 'node' on PATH
I am completely lost in understanding and fixing this issue in Visual Studio Code.
On OSX and VSCode 1.56.2 all I had to do was to close and restart VSCode and the problem went away.
first run below commands as super user
sudo code . --user-data-dir='.'
it will open the visual code studio import the folder of your project and set the launch.json as below
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app/release/web.js",
"outFiles": [
"${workspaceFolder}/**/*.js"
],
"runtimeExecutable": "/root/.nvm/versions/node/v8.9.4/bin/node"
}
]
}
path of runtimeExecutable will be output of "which node" command.
Run the server in debug mode.
To follow up, I just ran into this as well. When I installed Node.js there was an option that said Add to PATH (Available After Restart). Seems like Windows just needs a restart to make things work.
Quick fix that works for me. Navigate to the root directory of your folder from command line (cmd). then once you are on your root directory, type:
code .
Then, press enter. Note the ".", don't forget it. Now try to debug and see if you get the same error.
Apply a Default Node Version via NVM
I'm using macOS Big Sur and setting the default version via nvm fixed this for me by running this command: nvm alias default 16 (change 16 to the version you want as your default).
Note that node worked fine in the terminal for me (both with zsh and bash) but not when running via the vscode debugger and I already had the following config in both ~/.zshrc and ~/.bash_profile to load in nvm:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
It wasn't until I set the default Node version that vscode would launch node targets just fine.
I did which node on my terminal:
/usr/local/bin/node
and then i added
"runtimeExecutable": "/usr/local/bin/node" in my json file.
For me, the node binary is in PATH and I can run it from the terminal (iTerm or Terminal), and the Terminal apps are set to use zsh
If you are on a Mac, with iTerm and Zsh, please use the following VSCode settings for Node to work.
After this change, you can get rid of this line from your launch.json config file. (the debug settings in VSCode)
"runtimeExecutable": "/usr/local/bin/node"
If this doesn't work, make sure you choose the default shell as zsh. To do this,
Open the command palette using Cmd+Shift+P
Look for the Terminal: Select Default Shell command
Select zsh from the options
So node got kicked out of path. you can do
SET PATH=C:\Program Files\Nodejs;%PATH%
Or simply reinstall node to fix this. which ever you think is easiest for you
Had the same issue and in my case it was a problem with a vs code extension. Try running code as:
$ code --disable-extensions
Once in the editor, I ran my program in the debug mode and worked, and then started code with
$ code
And it continued working fine.
Hope it works for you.
I had a similar issue with zsh and nvm on Linux, I fixed it by adding nvm initialization script in ~/.profile and restart login session like this
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
I use /bin/zsh, and I changed vscode to do the same, but somehow vscode still use the path from /bin/bash. So I created a .bash_profile file with node location in the path.
Simply run in terminal:
echo "PATH=$PATH
export \$PATH" >> ~/.bash_profile
Restart vscode, and it will work.
I also ran into this error. Restart the PC works for me.
Do not launch the VS code from the start menu separately. Use
$Code .
command to launch VS code. Now, create your file with the extension .js and Start debugging (F5) it. It will be executed.
Otherwise, restart your system and follow the same process.
The cause for me receiving this error was trying the new pre-release VSCode JS debugger.
If you opted in, change via User settings:
"debug.javascript.usePreview": true|false
Everything in my normal configuration and integrated terminal was correct and finding executables. I wasted a lot of time trying other things!
(CMD+SHIFT+P) Shell command: Install 'code' command in PATH
should do the trick!
Just relaunch your project from the termial
e.g. yourprojectdir code .
i resolved this problem after disable ESLint extention.
I am on OSX, this did not work for me:
code . --user-data-dir='.'
but this DID work:
code . -data-dir='.'
This is the solution according to the VS code debugging page.
This worked for my setup on Windows 10.
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${file}"
}
The solution is here:
https://code.visualstudio.com/docs/editor/debugging
Here is the launch configuration generated for Node.js debugging
I also encountered this issue. Did the following and it got fixed.
Open your computer terminal (not VSCode terminal) and type node --version to ensure you have node installed. If not, then install node using nvm.
Then head to your bash file (eg .bashrc, .bash_profile, .profile) and add the PATH:
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
If you have multiple bash files, you ensure to add the PATH to all of them.
Restart your VSCode terminal and it should be fine.
Mine was more project specific. We use "Auto Reload" to run our launch.json for the backend. The error states the file path of the runtimeExecutable and allows you to open launch.json. In my launch.json case:
"runtimeExecutable": "${workspaceFolder}/functions/node_modules/.bin/nodemon"
I tried the restarts and answers here originally but no luck, so I manually explored into my functions/node_modules folder and realized .bin was missing. I used my terminal to path into functions like so:
cd functions
Terminal directory path example: ( ~/OneDrive/Desktop/{project dir covered}/{project dir covered}/functions )
Then I did an npm install using npm i, and now everything is back to normal. Keep in mind, most of these answers are general fixes. If your situtation is more specific, be sure to break it down from the start. Hope this might help others!

Resources