Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change node version with nvm

Tags:

shell

node.js

nvm

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.

like image 734
Bilal Dekar Avatar asked Dec 14 '18 18:12

Bilal Dekar


People also ask

How can I change Node version in NVM?

Switching among Node. 7; we can simply run either nvm use 12.22. 7 or nvm use 16.13. 0 to easily switch into either version we need. Note that since we only have one version that begins with 12, 14, or 16, we can switch versions with a simple nvm use 16 , nvm use 14 , or nvm use 12 command.

Can I change npm version with NVM?

Just go with nvm use node_version . That works fine for the first time. If you upgrade run npm i -g npm from an older node version and it updates to latest, your npm version will be the latest.

Can I switch Node version?

To change Node. JS versions, we have to first download the version we want. Make sure you have nvm installed first. If you don't know the version you want to install, type nvm ls-remote to get a full list of all installable Node.


3 Answers

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
like image 91
Derviş Kayımbaşıoğlu Avatar answered Oct 08 '22 02:10

Derviş Kayımbaşıoğlu


  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 information

    • Check nvm documentation for more information

    • Also you may need to specify a Node.js version for your IDE:

      Enter image description here

like image 148
Arseniy-II Avatar answered Oct 08 '22 01:10

Arseniy-II


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.

like image 46
Shubham Tiwari Avatar answered Oct 08 '22 02:10

Shubham Tiwari