Still worrying about managing the node version? Come and use nvm!

What is nvm and why use nvm

NVM (Node Version Manager) is a tool for managing Node.js versions. It allows installing and switching between multiple Node.js versions on the same computer. You can use NVM to test different versions of Node.js, or to use different Node.js versions in different projects.

The main value of NVM is that it allows developers to easily manage and switch between multiple Node.js versions. This is of great significance to developers, because different projects may need to use different Node.js versions due to component package issues, which greatly improves development efficiency and flexibility.

Install

Note that before installing nvm, the system should try to ensure that node has not been installed (uninstall it first), otherwise there may be some compatibility problems. I installed it on a new computer. I have not tried it in this case, but it is said on the Internet that it is not possible. For the foreseeable problem, interested friends can try it.

windows

download link

Select nvm-setup.exe below and click to download

image.png

After the download is complete, run the nvm-setup.exe file

Choose I accept the agreement (i accept the agreement), and then click next (next) along the way, after finishing (finsh)

Run the command line tool, which can be cmd or Git Bash. Execute nvm -v and the following information will be displayed to indicate that the installation is successful!

$ nvm -v
1.1.11
 

mac

Mac has two installation modes, 1. brew download, 2 I only tried the first one,

brew install nvm

$ brew install nvm
Warning: You are using macOS 13.
We do not provide support for this pre-release version.
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Twitter or any other official channels. You are responsible for resolving
any issues you experience while you are running this
pre-release version.

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/nvm-0.38.0.all.bottle.tar.gz
curl: (22) The requested URL returned error: 404

Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/nvm/manifests/0.38.0
Already downloaded: /Users/wanghao/Library/Caches/Homebrew/downloads/d4719fbfd26ca664d78c29b5c8c92ffd0e72cb3a984a968cd3073691e1e3559a--nvm-0.38.0.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/nvm/blobs/sha256:f3cd481b024f9e46a56450d6aaf43c83875e8e7796db6838a6441195b039a368
Already downloaded: /Users/wanghao/Library/Caches/Homebrew/downloads/37a2f0c239860a29bf8ed3deb2ae0c213357e3f96b3b2e68cc67cb2915d6ce67--nvm--0.38.0.all.bottle.tar.gz
==> Pouring nvm--0.38.0.all.bottle.tar.gz
==> Caveats
Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.

You should create NVM's working directory if it doesn't exist:

  mkdir ~/.nvm

Add the following to ~/.zshrc or your desired shell
configuration file:

  export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

You can set $NVM_DIR to any location, but leaving it unchanged from
/usr/local/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.

Type `nvm help` for further information.
==> Summary
  /usr/local/Cellar/nvm/0.38.0: 7 files, 176KB

Add the following at the end of the ~/.bash_profile file

    export NVM_DIR="$HOME/.nvm"
    [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
    [ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

then execute

use

View the list of node downloadable versions

Execute the command nvm list available to view the list of downloadable node versions


         $nvm list available

        |   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
        |--------------|--------------|--------------|--------------|
        |    20.3.0    |   18.16.0    |   0.12.18    |   0.11.16    |
        |    20.2.0    |   18.15.0    |   0.12.17    |   0.11.15    |
        |    20.1.0    |   18.14.2    |   0.12.16    |   0.11.14    |
        |    20.0.0    |   18.14.1    |   0.12.15    |   0.11.13    |
        |    19.9.0    |   18.14.0    |   0.12.14    |   0.11.12    |
        |    19.8.1    |   18.13.0    |   0.12.13    |   0.11.11    |
        |    19.8.0    |   18.12.1    |   0.12.12    |   0.11.10    |
        |    19.7.0    |   18.12.0    |   0.12.11    |    0.11.9    |
        |    19.6.1    |   16.20.0    |   0.12.10    |    0.11.8    |
        |    19.6.0    |   16.19.1    |    0.12.9    |    0.11.7    |
        |    19.5.0    |   16.19.0    |    0.12.8    |    0.11.6    |
        |    19.4.0    |   16.18.1    |    0.12.7    |    0.11.5    |
        |    19.3.0    |   16.18.0    |    0.12.6    |    0.11.4    |
        |    19.2.0    |   16.17.1    |    0.12.5    |    0.11.3    |
        |    19.1.0    |   16.17.0    |    0.12.4    |    0.11.2    |
        |    19.0.1    |   16.16.0    |    0.12.3    |    0.11.1    |
        |    19.0.0    |   16.15.1    |    0.12.2    |    0.11.0    |
        |   18.11.0    |   16.15.0    |    0.12.1    |    0.9.12    |
        |   18.10.0    |   16.14.2    |    0.12.0    |    0.9.11    |
        |    18.9.1    |   16.14.1    |   0.10.48    |    0.9.10    |
        ...

Download the execution version node

The column used by LTS is the node version

Execute the command nvm install 18.16.0 to install the node with the version number

$ nvm install 18.16.0
Downloading node.js version 18.16.0 (64-bit)...
Extracting node and npm...
Complete
npm v9.5.1 installed successfully.


Installation complete. If you want to use this version, type

Specify the node version currently in use

nvm use 18.16.0

nvm use 18.16.0
Now using node v18.16.0 (64-bit)

View the downloaded node version and the currently used version

nvm list nvm use 18.16.0 Now using node v18.16.0 (64-bit)

$ nvm list

  * 18.16.0 (Currently using 64-bit executable)
    16.20.0

Finally check if the node version has been switched

$ node -v
v18.16.0

common problem

1 The official website address link cannot be opened

But by the wall

Solution

1> bring your own ladder

2> Try to use Thunder to download the following link

nvm-setup.exe

2 After executing the switching command, why node -v still displays the previous version

Generally, it is a permission issue. Command line tools (cmd, Git Bash) should be opened with super administrator privileges before executing the switching command

image.png

3 After switching the node version, why some commands cannot be used

Because different node versions are isolated independently, only one version can be used at the same time, and the corresponding global dependency packages installed are only limited to the current version. For example, some scaffolding command line tools are only limited to the corresponding node version. , when the version number is switched, the dependencies must be re-downloaded

Guess you like

Origin juejin.im/post/7243263236623384634