225

I'm using Yeoman to create a project. When I try to use Gulp.js I run the command gulp serve. An error tells me that I need an older version of Node.js (8.9.4), knowing that I've installed the latest version (10.14.1).

So I installed nvm to change the Node.js version. I had to set it into path C:\, and then I run with success: nvm install 8.9.4. And when I try to use it, nvm use 8.9.4, it’s always the latest version that is used:

Enter image description here

If I try to use 8.10.0 and then run node -v, it tells me access refused, and the same to any Node.js command.

Arseniy-II
  • 8,323
  • 5
  • 24
  • 49
Bilal Dekar
  • 3,200
  • 4
  • 28
  • 53
  • try running in administration cmd – Deepak Kumar Jul 01 '19 at 14:03
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and do the right [thing](https://stackoverflow.com/posts/53785383/edit) (it covers command line and program output as well). – Peter Mortensen Aug 09 '22 at 15:54

5 Answers5

281

nvm install 8.10.0 is for installing proposed node version locally.

In order to use it:

nvm use 8.10.0

Note that you need to run this command as administrator.

You can always set default Node.js version:

nvm alias default 8.10.0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • 1
    i tried to use 8.10.0, then when i run node -v it tells me access refused, same to any node command – Bilal Dekar Dec 14 '18 at 19:05
  • 1
    use it with elevated permission. run cmd as Administrator – Derviş Kayımbaşıoğlu Dec 14 '18 at 19:07
  • in cmd.exe execute `where node` go to the folder and try to execute there with elevated permission. I believe that this is either permission issue or you may have another node installation which nvm could not alter. – Derviş Kayımbaşıoğlu Dec 14 '18 at 20:36
  • 4
    is admin privileges need to make it "stick"? because it runs fine without root, but the next time i open a shell it reverts back to the old version – Michael Sep 29 '19 at 00:21
  • @Michael `tail -n2 ~/.bashrc nvm use v14.18.2 ` You could add the above line to your .bashrc and each login it will set locally – V H Dec 06 '21 at 15:37
163
  1. Install

    nvm install 8.10.0
    
  2. Use once per terminal

    nvm use 8.10.0
    
  3. Set up as the default for all terminals

    nvm alias default 8.10.0
    
  4. Set up Node.js version for your IDE

Enter image description here

  1. For more information check nvm documentation
Arseniy-II
  • 8,323
  • 5
  • 24
  • 49
52

Switch to a specific Node.js version

nvm use 8.10.0

Switch to the latest Node.js version

nvm use node

Switch to the latest LTS version

nvm use --lts

You can check which versions you have installed by running:

nvm ls

The entry in green, with an arrow on the left, is the current version in use.

Specify a Node.js version on a per-project basis

Version managers, such as RBEnv, allow you to specify a Ruby version on a per-project basis (by writing that version to a .ruby-version file in your current directory). This is kind of possible with nvm in that, if you create a .nvmrc file inside a project and specify a version number, you can cd into the project directory and type nvm use. nvm will then read the contents of the .nvmrc file and use whatever version of Node.js you specify.

If it’s important to you that this happens automatically, there are a couple of snippets on the project’s home page for you to add to your .bashrc or .zshrc files to make this happen.

Here’s the Z shell (executable zsh) snippet. Place this below your nvm configuration:

    autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

if [ "$nvmrc_node_version" = "N/A" ]; then
  nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
  nvm use
fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

When you change into a directory with a .nvmrc file, your shell will automatically change the Node.js version.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shubham Tiwari
  • 1,761
  • 11
  • 19
  • I have multiple versions installed in the build server & different applications requires different versions, So nvm use x.xx did the job – Shan Feb 03 '22 at 10:04
8

Make sure you run your terminal as an system administrator

Then run the following command

nvm use <version>

In my example i ran nvm use 16.14.0.

You will need to have already installed the version that you want to use.


Without the privilege, I was getting this error:

Output

exit status 5: Access is denied.
JGallardo
  • 11,074
  • 10
  • 82
  • 96
0

You need to edit your .bashrc file.

Add the below to that file. Change the version to your preferred version. This example uses v16.13.1.

It is possible you have something like this already in that file which causes the change back to your previous versions.

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
export PATH="/home/zentech/.local/bin:/home/zentech/.nvm/versions/node/v14.18.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nafiu Lawal
  • 447
  • 1
  • 7
  • 16