How to change node version with nvm

node.jsNvm

node.js Problem Overview


I'm using yeoman to create a project, when I try to use gulp I run the cmd : gulp serve an error tells me that I need an older version of node (8.9.4), knowing that I've installed the latest version (10.14.1).

So i installed nvm to change the node version, I had to set it into path C:\, then I run with success : nvm install 8.9.4 and when I try to use it : nvm use 8.9.4, its always the latest version that is used

enter image description here

if i try to use 8.10.0, then run node -v it tells me access refused, same to any node command

node.js Solutions


Solution 1 - node.js

you need to use

nvm use 8.10.0

note that you need to run this command as administrator

Solution 2 - node.js

1) Install (root permissions might be required)

nvm install 8.10.0

2) Use once per terminal (root permissions might be required)

nvm use 8.10.0

3) Set up as default for all terminals (root permissions might be required)

nvm alias default 8.10.0

4) Additional info

  • check nvm documentation for more info

  • Also you may need to specify node version for your IDE:

enter image description here

Solution 3 - node.js

Switch to 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 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 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 ZSH snippet. Place this below your nvm config:

    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 Node version.

Solution 4 - node.js

Make sure you run your terminal as an admin

nvm use <version> // this should work fine

without the privilege, I was getting this error

nvm use 16.14.0 exit status 5: Access is denied.

Solution 5 - node.js

You need to edit your .bachrc file.

add the below to the that file. Change the version to your preferred version. This example uses v16.13.1. It is possible this 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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBilal DekarView Question on Stackoverflow
Solution 1 - node.jsDerviş KayımbaşıoğluView Answer on Stackoverflow
Solution 2 - node.jsArseniy-IIView Answer on Stackoverflow
Solution 3 - node.jsShubham TiwariView Answer on Stackoverflow
Solution 4 - node.jsTlotli OtlotlengView Answer on Stackoverflow
Solution 5 - node.jsNafiu LawalView Answer on Stackoverflow