Visual Studio Code to use node version specified by NVM - node.js

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

Related

What is the best way to automatically switch between node versions?

When you work with two projects and one project depends on node v 8 and another one project depends on node v 10 you have to somehow switch between them.
Not convenient way:
This is not convenient because i have to always remember to type nvm use when i've just entered into root of project dir or when i've just opened new terminal session.
So when you are working on some project you can go in to root dir and run nvm use and it'll pick up node version from your .nvmrc and you can work on this project in this terminal session.
The same thing with another project - nvm use and work in this terminal session.
Convenient but not reliable way:
This is not way reliable because not all of devs have nvm exactly here $HOME/.nvm/nvm.sh and i don't like this hardcoding of nvm path because it's looks like some dirty hack.
Follow answers in this thread and get some working way. This means that just for my personal local developing it will work(i've tried this thread and it's not working because of this error and i don't know what it means)
$ source $HOME/.nvm/nvm.sh && nvm use && nodemon ts-node -r
tsconfig-paths/register src/main.ts nvm is not compatible with the
"PREFIX" environment variable: currently set to "/usr/local" Run
unset PREFIX to unset it.
Engines in package.json way
I've tried write "engines": {"node" : "10.14.1"}, in to my package.json but when i've started yarn - node haven't changed to 10.14.1(it's already installed on my laptop)
I don't know any more ways to conveniently and reliably change my node version just running yarn start. Do you know some way? Or may be you know some best practices?
After some time i've reinstalled my OS and i've tried to use ZSH terminal. There is zsh-nvm plugin which check .nvmrc every time you do cd. Convenient. So good so far.

Npm command not found in visual studio code

Please tell me how i can solve it. Iam using window 7 32 bit:
By default, Visual Studio Code runs shell commands like npm in a loginless shell. If you installed NVM, Visual Studio Code may have no indication where to find npm to run it.
Put the following lines are in .bash_profile: (Note: Not .bashrc.)
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
Add this setting to settings.json:
"terminal.integrated.shellArgs.linux": ["-l"]
On Mac OS , I switched to the zsh and got this fixed. These following steps fixed the issue :
On your VsCode in Mac : shift + command + P .
On the Prompt > type : Terminal: Select Default Profile , then "Click it". Note, as you type you will find this option in the auto-complete .
Click the option for zsh or your desired shell.
Restart VSCode .
now npm will work in your vscode terminal.
I too had this issue.
To solve this follow the below steps.
Make sure you have npm installed
- go to command prompt & type npm -v
- if a version comes out it's installed, else go to https://nodejs.org/en/ and download same.
Then come to vs code and set deafult shell to cmd.
to do so,
- Press Ctrl+Shift+P and type Select Default Shell
- Select Command Prompt.
- Press Ctrl+`
- Type npm -v and see npm works. :)
I got this error after having just installed nodejs/npm, and the way i resolved, while still being able to use git bash terminal in vscode was to simply restart my computer (I hadn't done this after node installation).
I needed to kill the terminal in VS Code and restart a new terminal to get npm to show as installed.
As you're aware, you can configure your environment variables in windows at 2 levels.
At user level
At system level
When VS code is started, it picks the variables in path at a user level and not at system level because you haven't run the application as Administrator.
Just copy-paste your node path from System Variables to User Variables
This does the trick for windows.
Thanks.
If you just installed your node while your VScode was running. Restart your vscode it should start working.
You need to install npm first, https://www.npmjs.com/get-npm and make sure npm command is accessible using terminal/command prompt.
You can also use https://marketplace.visualstudio.com/items?itemName=eg2.vscode-npm-script This extension supports running npm scripts defined in the package.json file and validating the installed modules against the dependencies defined in the package.json.
On mac, I switched the default shell from bash to zsh and it fixed the issue.
I'm gonna add an answer just for help others because this question is 2 years old.
If you can run the npm start (NPM SCRIPTS below the Explorer view) but you are not able to launch the command with a shortcut: Then check npm extension is installed and/or enabled for the current workspace/folder.
Otherwise vsCode will not be able to run the script and you're going to get a message in a little box like this
command 'npm-script.start' not found
Maybe the NPM Scripts View below the explorer is not available without the plugin I'm not sure
For me, restarting VS code and even my PC didn't work, but after restarting VS code via "reload" (not restart, not refresh) from ctrl+shift+P, then in the top right of the terminal clicking the leftmost button and clicking "kill terminal" then restarting VS code it worked.
I had this issue too and I'm running QubesOS which uses virtualization.
I could manage to get it running until I'd a standalone VM.
That solved my problem.
Just mentioning for the ppl using virtualization like virtual box or VMware.

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!

Setting up paths in Sublime with NVM

I am using NVM as my node version manager on OSX, which is great except that it creates multiple problems with my IDE. I am using Sublime Text and most of the plugins that I have ever used look for nodejs at /usr/local/bin and since I am using NVM node is obviously not available at that location, instead available at /Users/${User}/.nvm/${NodeVersion}/bin/node.
I have an option of updating the path of nodejs in that plugin's configuration but then when I install another plugin I have to do the same thing. Same applies to using CoffeeScript, LESScss etc.
Moreover when I update my node version via NVM, I have to update the paths in all the configs again. NVM has quite essentially created more problems while trying to solve one for me.
UPDATE:
After a very long time I was able to finally resolve the problem using isaacs brilliant solution - nave. The nave usemain stable command, is just enough to understand you sentiments :)
New Answer for 2020!
You can now add the following one-liner to your .bashrc, .zshrc, etc.
export NVM_SYMLINK_CURRENT=true
Then, anywhere you need your node path, it's always in the same place:
~/.nvm/current/bin/node
#OR
$HOME/.nvm/current/bin/node
Further reading: https://medium.com/#danielzen/using-nvm-with-webstorm-or-other-ide-d7d374a84eb1
I was having a similar problem yesterday. I found a python script that exports my Node path (the nvm node path) for Sublime. I updated it for OSX and ST3. You can find it here:
https://gist.github.com/joesepi/11269417
Drop that script in your Packages dir, the path for ST3 is in the comments in the script. You may have to restart ST as well. And if you update your node version, you will need to update that script too. I hope that helps!
you can use command "nvm which node-version " such as
nvm which 0.12.0
Install NVM from github.com/xtuple/nvm
Run this command to install NVM as global and you'll see everything works fine
wget -qO- https://raw.githubusercontent.com/xtuple/nvm/master/install.sh | sudo bash
I'm using n. I also tried nave but I was having trouble with it with nvm installed. Hope this helps.
For Linux users,
By default nvm write some node paths in your .bashrc and there are loaded only if you’r in bash mode.
So just you need to move the two paths-line to the bottom on the file like that :
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# Set the NVM path for my Sublime before return.
export NVM_DIR="/home/<yourUser>/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
CURRENT_NODE_VERSION=$(nvm current); nvm which $CURRENT_NODE_VERSION
I am using ubuntu 18 and this was how i was able to solve it
open your command prompt and access /home/{username}/.config/sublime-text-3/Packages create a new file and put this code
# $HOME/.config/sublime-text-3/Packages/node-env.py
import os
import getpass
nvm_path = '/home/%(user)s/.nvm' % {'user': getpass.getuser()}
nvm_default_file_path = '%(root)s/alias/default' % {'root': nvm_path}
nvm_node_root = '%(root)s/versions/node' % {'root': nvm_path}
# Grab default alias
with open(nvm_default_file_path, 'r') as content_file:
content = content_file.read()
# Prepend 'v' to match folder names
version = content.strip()
if version[0] != 'v':
version = 'v' + version
# Take highest valid folder name
versions = os.listdir(nvm_node_root)
found = sorted([v for v in versions if v.startswith(version)])[-1]
if found == None:
print("Failed to configure node: no valid version found for %(version)s" %{'version': version})
else:
print("Configure node: %(version)s" % {'version': found})
node_path = "%(root)s/%(version)s" % {'root': nvm_node_root, 'version': found }
print("Node path: %(root)s" % {'root': node_path})
path = "%(root)s/bin:%(root)s/lib:%(path)s" % {'root':node_path,'path':os.environ["PATH"]}
os.environ["PATH"] = path
save this file as "node-env.py".
It helps solve issues with most JavaScript libraries like Typescript plugging and others.

Is there a way to have node preserve command line history between sessions?

When I run node from the command line with no arguments, I enter an interactive shell. If I execute some commands, exit node, and restart node, the up arrow doesn't do anything (I'd like it scroll through my previous commands).
Is there a way I can invoke node interactively such that it will remember my old commands?
You could use rlwrap to store node.js REPL commands in a history file.
First, install rlwrap (done easily with a package manager like apt-get or brew etc).
Then add an alias for node:
alias node='env NODE_NO_READLINE=1 rlwrap node'
I'm on OSX so I add that alias to my ~/.bash_profile file, and would reload my bash_profile file via source ~/.bash_profile.. and I'm good to go!
Hope this helps!
I found a nice little project, which solves the problem:
https://www.npmjs.org/package/repl.history
install using npm (npm install -g repl.history)
and run repl.history on the command line.
io.js 2.0 includes support for persistent REPL history.
env NODE_REPL_HISTORY_FILE=$HOME/.node_history iojs
The maximum length of the history can be set with NODE_REPL_HISTORY_SIZE, which defaults to 1000.
In io.js 3.0+, REPL history is enabled by default and NODE_REPL_HISTORY_FILE is deprecated in favor of NODE_REPL_HISTORY (default: ~/.node_repl_history).
Node supports this natively.
When in node REPL just issue the following command to save the history:
$> .save ./file/to/save.js
Reference: Commands and Special Keys
As well check file .node_repl_history in user home directory.
I like the combination of both dreampulse's and badsyntax's answers. With repl.history, plus an addition to my .bash_profile I get the behavior I expect, which is command history and syntax highlighting in the node interactive shell, but bypassing repl when called with arguments (to run a script).
npm install -g repl.history
Then edit your ~/.bash_profile, adding:
function node(){
if test "$#" -lt 1; then repl.history
else env node $#; fi; }
Now restart your shell or run . ~/.bash_profile and you're good to go.
Now running
$ node
will open the repl.history nodejs interactive shell, and
$ node program.js [...]
will run program.js with node as expected.

Resources