192

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
A G
  • 21,087
  • 11
  • 87
  • 112
  • Related (and possible duplication): https://stackoverflow.com/questions/24585261/nvm-keeps-forgetting-node-in-new-terminal-session – Gyuri Oct 02 '17 at 02:37

34 Answers34

174

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"
    }
]}
Tjaart van der Walt
  • 5,149
  • 2
  • 30
  • 50
Sm Srikanth
  • 1,992
  • 1
  • 10
  • 10
  • 10
    I was going to ask how Code knows where to find this version, but apparently, this option was added specifically for nvm. https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_multi-version-support – Brian Witt May 28 '19 at 12:28
  • 7
    Where is the `launch.json` file? – Petrus Theron Feb 28 '20 at 08:47
  • 2
    @PetrusTheron if you don't have one already, you'll have create one. There are instructions here: https://code.visualstudio.com/docs/editor/debugging#_run-view – Joel Glovier Apr 28 '20 at 20:43
  • 6
    Setting "runtimeVersion" to "default" solved mine. – Nnanyielugo Jan 25 '21 at 14:18
  • That looks super promising, but does not work for me. may need to update nvm, I donno. – LMS5400 May 16 '22 at 23:58
  • My type is `npm` since i'm running an `npm` command. Not a node command. And runTimeVersion is not available for it. – AlxVallejo Jul 11 '22 at 13:50
  • This worked for me by adding "launch" configuration in my settings.json and setting the runtimeVersion to "default": "launch": {"configurations": [ { "type": "node", "runtimeVersion": "default", "request": "launch", "name": "Launch" } ], } – sazzledazzle Mar 19 '23 at 17:28
  • Thx guys, worked in my env where I have different node versions installed with nvm. – Rodolfo Gonçalves Jun 08 '23 at 18:28
  • This worked for me by first adding a .nvmrc file to the repo with the desired version of node, and then creating a launch.json with NO runtime version. This is nice because node version is stored in a single place and can be updated more easily. – Little Brain Aug 08 '23 at 10:42
169

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.

A G
  • 21,087
  • 11
  • 87
  • 112
  • 1
    This worked for me as well, but there should be an easily way to specify the path to node globally for VSCode. – jr. Feb 09 '18 at 16:33
  • 14
    This did not work. After aliasing, I have to use `nvm use default` everytime I use a new terminal – Ramesh Pareek Sep 12 '18 at 09:38
  • Didn't work for me either. Nor did using `nvm use default`. – Matt Sanchez Oct 29 '18 at 14:23
  • This worked on my Mac Mini but not on my MacBook Air. I'm not a shell/PATH expert but I think both machines have their PATH configured differently. – Matt Sanchez Nov 07 '18 at 19:22
  • RE: Update (12/04/2018): The solution by @Charlyboy, editing the PATH, worked for me using `node -v` now returns `v11.4.0`. Thanks for the update, perfect timing =] – IAmMearl Dec 08 '18 at 17:25
  • 3
    I had to remove my brew-installed version of node before this worked. – samlandfried Jan 06 '19 at 04:41
  • In my case, i tried the first answer (set default ....) and it didn't work, but then i restarted VS Code, and it worked. – ElHombre55 Apr 02 '19 at 19:21
  • 2019 this works for me thank you! I just had to remember to start node after using nvm default – Jonathan Oct 31 '19 at 03:24
  • 4
    I'm running VS Code with WSL. After setting the default alias, it is necessary to restart VS Code to get it to pick up the change. – John Mills Feb 21 '20 at 03:56
  • If you are using VS Code's Dev Containers, then it will use the runtime in the container marked `default` by nvm, so the alias trick works here too. However, the `type` of the launch config matters. `pwa-node` refers to node on the host system. `node-terminal` refers to the container. – I'll Eat My Hat Jun 15 '20 at 10:42
  • 2
    If this doesn't work for you, make sure you're creating new terminal after VS Code restart. If VS Code restores your previous console context it doesn't pick up the new default node version. – FINDarkside Jan 26 '22 at 13:54
  • 1
    Worked for me, the simplest solution. – Kaushal Pahwani Apr 06 '22 at 08:48
  • 1
    In my case it worked by running this in vs code terminal: `nvm use 16.14.2` and then restart VS Code. Beautiful! – Adriana Hernández Apr 19 '22 at 08:39
72

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"
}
Alongkorn
  • 3,968
  • 1
  • 24
  • 41
45

Some of the answers provided are correct and upvoted, but somewhat incomplete. This procedure worked for me:

  1. Open the terminal window inside VS Code and run node -v. You'll get for instance v10.12.0.
  2. Open a terminal window outside VS Code Change your node version with nvm (ie. nvm use v12.14.0)
  3. Cmd+ Shift + p and choose Preferences > Open Settings (JSON)
  4. Add "terminal.integrated.shellArgs.osx": [] to your user config
  5. Cmd+ Shift + p and choose Shell Command: Install 'code' command in PATH
  6. Close VS Code.
  7. Open a terminal window and run code. This will open VS Code with a new and updated bash / zsh session.
  8. 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
Wallace Sidhrée
  • 11,221
  • 6
  • 47
  • 58
  • 1
    This did not work for me. Still set to 12.19 when nvm version is 14.16, will try the nvm default method next. – secondman Apr 04 '21 at 20:22
  • Thank you so much! Small suggestion. The setting property is deprecated. In my case this worked: "terminal.integrated.defaultProfile.osx": "zsh" – Sanchitos Feb 14 '22 at 22:22
43

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 to 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.

Community
  • 1
  • 1
Charlyboy
  • 549
  • 4
  • 6
  • 3
    This should be the accepted answer. I was previously setting the `runtimeVersion` in launch.json but that only sets the node version for a specific task. This works throughout the integrated terminal instance. Thanks! NB. I had to set the PATH var in `.zshrc` since I'm using zsh for it to work – timiscoding Aug 15 '19 at 06:26
  • After reading the alternative link provided - which suggested the problem was with nvm and the way it handles subshells - i updated nvm to v0.34.0 and it works without the reset path workaround. – timiscoding Aug 15 '19 at 06:48
30

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)"
BlueTabasco
  • 401
  • 4
  • 5
  • 3
    I am also using oh-my-zsh and only this solution worked for me. thank you so much. Now I don't have to change node version every time I open VS Code. – Rameshwor Maharjan Jul 03 '19 at 06:19
  • 3
    I had the same issue, since macos switched to zsh after nvm (and the vscode plugin) was already installed and working. By the way, I had to restart VSCode (not just reload) to refresh the env. – tutuDajuju Aug 10 '20 at 09:19
  • 2
    This was the only one that worked for me. Thank you! – Rafael Rozon Aug 10 '20 at 16:18
  • 1
    using the named aliases was working as a workaround for this issue, but this is the only thing that's actually solved the issue for me. – Mugshep Dec 01 '21 at 12:36
  • 1
    Same, this is the only thing that worked me. – mijota Aug 26 '22 at 17:42
27

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": []

Skeevs
  • 336
  • 3
  • 4
21

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".

enter image description here

And then launch a terminal and select your node via nvm and then launch "code".

enter image description here

jr.
  • 4,503
  • 7
  • 44
  • 62
  • I used this, and found out that if you go to the VScode tool bar and bring up the 'about' page, it is still displaying the old version, but if I use the command line, it will tell me it is pointing to the upgraded version, so it sort of works. – Harvey Lin Sep 17 '18 at 18:08
  • This requires one to close VSCode and restart it from the command line to work effectively. – Wallace Sidhrée Mar 02 '20 at 11:33
  • 2
    Accepted answer didn't help, but running `nvm use default` in VSC afterward did. Thanks! – Richard Morgan Aug 20 '21 at 18:54
  • So my issue was just to launch VS code with the version specified exactly in project's nvmrc. At the same time I don't want to change default nvm version, because I need a separate node version only for one project. Opening VSCode directly from the project directory with `npm use` solved my problem. Thank you a lot! – Igor Skobelev Oct 01 '21 at 21:32
19

Lots of complicated answers here. In my case, this was caused by Node previously having been installed. Fixed it by deleting the following directories:

rm -rf /usr/local/bin/npm
rm -rf /usr/local/bin/node

Then running nvm use default in VS Code to pick up the Node version installed by nvm.

Víctor
  • 850
  • 7
  • 19
Richard Morgan
  • 585
  • 6
  • 10
11

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.

Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38
  • 1
    This solution helped me the best. I didn't need to open code from terminal either. – Matt Scheurich May 19 '20 at 06:15
  • 1
    This solution works well on Windows too, as setting that runtime parameter appears to make eslint look at the system path variable to resolve the node binary rather than using the internal version included with vscode. – nextgentech Mar 02 '21 at 04:23
  • 1
    SO HARD TO FIND THIS, UNBELIEVABLE. – Eugene Jan 21 '23 at 09:39
  • Kind of disappointing that the eslint extension DOESN'T use the `node` it finds on the path by default. – Marc Jan 25 '23 at 12:17
8

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.

rsimp
  • 791
  • 9
  • 9
  • This provided the most seamless experience for me. I can place .nvmrc files in my projects that rely on different Node versions. The contents of the .nvmrc should be exactly the version and nothing else. i.e. "v14.20.1". I added the above shell snippet into my .bash_profile and .bashrc files, since there was some issues with VSCode and when integrated terminals were loaded. – TheBrockEllis Nov 01 '22 at 20:09
6

Particularly with the shell I had no problems, but you may:

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.

estani
  • 24,254
  • 2
  • 93
  • 76
6

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
Craig Hicks
  • 2,199
  • 20
  • 35
4

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
madflanderz
  • 1,159
  • 2
  • 7
  • 7
4

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.

Franz Wong
  • 1,024
  • 1
  • 10
  • 32
4

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
Mateja Petrovic
  • 3,799
  • 4
  • 25
  • 40
3

So, your nvm is configured well, but other version of node STILL keeps taking over?

Remove all non-nvm versions of node:

  1. brew uninstall --force node (yarn will be fine without system node)
  2. Other version installed from pkg or other non-nvm method
  3. 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

Stepan
  • 1,136
  • 12
  • 14
  • For the love of Pete, there is no need to use `brew` to install node. It has a native installer! https://nodejs.org/en/download/ – jnovack Sep 27 '20 at 20:45
  • @jnovack My answer covers *uninstalling* brew-installed version of node. Please re-read it. Additionally, using `brew` to install node is quite fine for people who don't need `nvm` and has advantages over native installer. – Stepan Oct 07 '20 at 03:17
3

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",
        },
    ]
}
Micael Mota
  • 433
  • 3
  • 7
  • 1
    You could also add `[ -s "./.nvmrc" ] && nvm use` to your ~/.bashrc or ~/.zshrc to achieve the same affect on all projects. Precheck for .nvmrc so no command issued for projects that don't have one – rsimp Mar 28 '22 at 15:41
3

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:

  1. create the .nvmrc file at the root of my project that old the node version I want to use as stated in nvm ReadMe
  2. adding the automatic activation script in my ~/.zshrc also in the ReadMe (bashrc script also in the readme)
asiera
  • 492
  • 5
  • 12
  • When I add this script to my .zshrc I get the "Unable to resolve your shell environment in a reasonable time" error from VS Code – paulwithap Mar 23 '22 at 19:14
  • when you do `nvm version` in your terminal, does it work? – asiera Mar 24 '22 at 00:13
  • For a more lightweight solution just add `[ -s "./.nvmrc" ] && nvm use` to the bottom of your `.zshrc`, `.bashrc` etc – rsimp Mar 25 '22 at 19:44
3

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:

Visual Studio Code NPM script runner

Step-by-step

  1. Place an .nvmrc file containing the project's Node version into the root folder of your project.

  2. Enable automatic environment as described here: https://github.com/nvm-sh/nvm#deeper-shell-integration.

  3. 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):

enter image description here

And running an NPM script triggers NVM:

enter image description here

enter image description here

Cheers!

Niels
  • 372
  • 2
  • 15
  • 1
    Exactly what I was looking for, to have nvm settings automatically respected in my VS Code React Native project, thanks! – Justin Dec 23 '22 at 16:45
  • 1
    The other answers dont work for npm script runner tasks in vs code. This answer works because of the `terminal.integrated.automationProfile.osx` field. Thanks. – iprateekk Jan 03 '23 at 13:02
2

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:

Quang Van
  • 11,475
  • 12
  • 57
  • 61
  • For me setting just this worked:`"terminal.integrated.defaultProfile.osx": "zsh"` no need of `"terminal.integrated.profiles.osx"` – Nishant Sep 16 '21 at 05:23
2

following solution worked for me

  1. first install and use the desired node version with nvm with these commands: nvm install v16.13.1 and nvm use v16.13.1.
  2. 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
  3. 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"
        }
    ]
}
kiranr
  • 2,087
  • 14
  • 32
2

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

Andy
  • 2,892
  • 2
  • 26
  • 33
2

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>
Chukwunazaekpere
  • 906
  • 1
  • 6
  • 13
1

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.

Ananthu
  • 31
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – mohammad mobasher Feb 05 '22 at 07:08
  • Finally got a proper solution for this. I had a random npm version in /usr/local/lib that VS Code was defaulting to, which then messed up the other things. – seaders Jan 16 '23 at 16:19
0

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.

michelepatrassi
  • 2,016
  • 18
  • 32
0

None of the other solutions worked for me.

So I ran nvm alias default node and this fixed it for me.

Harshit Ruwali
  • 1,040
  • 2
  • 10
  • 22
coinGuyBri
  • 17
  • 3
  • 3
    Beware: `nvm alias default node` sets the MOST CURRENT version of node installed, not a specific version you want. – jnovack Sep 27 '20 at 20:47
0

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:

  1. Click the down arrow on the terminal dropdown
  2. Select Default Shell
  3. Select 'bash'
  4. Try node -v and that should return the correct version that was set as default nvm alias default v12.14.0
Nikolay Advolodkin
  • 1,820
  • 2
  • 24
  • 28
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

Leslie C
  • 43
  • 6
0
sudo rm -rf /usr/local/opt/node@<YOUR_NODE_VERSION>

then restart the Visual Code

Ba Tới Xì Cơ
  • 482
  • 4
  • 16
0

If one uses nvm, then this may work out:

In vscode setting.json change typescript key like this:

  "code-runner.executorMap": {

    // "typescript": "ts-node",
    "typescript": "node -r ${NVM_BIN}/../lib/node_modules/ts-node/register",

If it works for you, then here is my explanation.

First I tried to just put it

${NVM_BIN}/ts-node/register

but that didn't work. Then I looked inside the directory and found out that ts-node there is a symlink:

ts-node -> ../lib/node_modules/ts-node/dist/bin.js

So, I guess that is why plain 'ts-node/register' is not resolving properly, because it actualy becomes 'ts-node/dist/bin.js/register' which shouldn't work.

Hope that might help someone.

Hero Qu
  • 911
  • 9
  • 10
0

I needed to vs code to be aware of the node version, not the terminal. Starting code from a terminal is an option, but if you forget...

A much more flexible way is to use automator to create the environment you need before starting vs code.

Run Automator and start a new Application project. Just add a Shell-Script (under Utilities) and add the following line to create a "normal" (login+interactive) shell, where you may start code:

zsh -lic code

This is how it looks like: automator-shell script

Save it in your Application folder with a name you'l find it every time you look for vs code (I either type vs or code, so I called it "vs code runner")

The bonus here is that you can apply other configurations too. the idea is the same:

  1. setup the environment
  2. starat code
estani
  • 24,254
  • 2
  • 93
  • 76
0

My solution to this was as follows - opening a terminal node reported correct version as specified by nvm. Opening a terminal in VS Code it defaulted to the "default" node version.

Therefore the simplest solution for me was:

edit ~/.zshrc After the export of NVM_DIR, that was automatically added, add the following line:

nvm alias default `node -v`

Thus every time login it resets the default node version. If you run nvm use 14 then run source ~\.zshrc or the above reset alias command, then in VSCode should always have correct version when opening terminal.

Json P
  • 11
  • 3
-1

Posting it in June 2023 because there are lots of answers telling to re-install node and asking to make lots of configuration changes. In fact the solution is just to Restart your VSCode!

Just make sure that your Mac/PC Terminal have the default node version set as per your need and then restart the VSCode. Best tool to switch between the Node Versions is NVM.

  1. You can check your available NODE Versions just by doing nvm list
  2. Set the Default using nvm alias default v18.00.0.
  3. Or install different version of Node using nvm install v18.00.0

Cheers!

Dynamic Remo
  • 421
  • 9
  • 20