Recently, while working with NPM, I have noticed that the global installation always encounters permission-related errors, so I always have to add sudo in front of it and keep typing in passwords.

Laziness makes me progress. After googling the solutions to related problems, I find that NPM has already stated How to Prevent Permissions Errors in the official documents. According to its description, the root cause is that the Node.js installation package installs the program on an address with only local permissions (writes require administrator permissions). There are two ways to fix it, and the recommended one is the NVM approach described in this article.

The advantage of NVM

Node Version Manager (NVM) solves two problems through centralized Version management of Node and NPM

  1. NVM provides a convenient mechanism for installing multiple versions of Node and NPM on the same terminal and the ability to quickly switch versions, which greatly helps us detect project compatibility and locate bugs.
  2. The various permission issues mentioned at the beginning of this article have been resolved so that you never have to re-type sudo for a global installation.

The use of NVM

Here is a brief introduction to the installation and use of NVM on MacOS. (Note: NVM is mainly aimed at MAC OS and Linux users. For Windows users, please see here.)

Prior to installation

Before installing, you should first consider whether you want to keep the original system installed Node. NVM can be installed with the original node and provides a way to cut back to the System node.

Note, however, that if you have multiple users on your system, the NVM currently installed is for you only, and other users can only use the installed Version of Node if they don’t have it installed. Both global installation node modules address is not the same (/ usr/local/lib/node_modules / * v ~ /. NVM/versions/node/vX 7.0.x.x/lib/node_modules / *). There is a risk of version inconsistencies to some extent.

If you choose to uninstall the original system node, here is a very useful explanation

The installation

The installation method is relatively simple, just execute the following script. The script clones the project code and sets the environment variables. If there are problems, check out Trouble Shooting on Github.

The curl - o - https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bashCopy the code

After the installation, turn off the terminal and start a new one, and enter the following commands to verify the installation.

command -v nvm       # should return nvm
Copy the code

Note: You can use the same script to automatically update the NVM

use

After installation, it is very simple and straightforward to use. Here are some common functions

1. Install a certain version of Nodenvm install

nvm install --lts             Install the latest LTS version of Node
nvm install node              Install the latest version of NodeNVM install 6.14.4Install Node version 6.14.4
Copy the code

2. List all node versions

nvm ls                    # Native all versions
nvm ls -remote             # Remote all versions
Copy the code

3. Switch to or use a certain version of Nodenvm use

nvm use --lts             Use the latest LTS version of Node
nvm use node              Use the latest version of NodeNVM use 6.14.4# Use Node version 6.14.4
Copy the code

4. Start nodenvm run

nvm run --lts             Start the latest LTS version of Node
nvm run node              Start the latest version of NodeNVM run 6.14.4Start Node version 6.14.4
Copy the code

5. Use a certain version

nvm execThe node - version 4.2Copy the code

6. Query the installation location of a version

nvm which 5.0
Copy the code

Node, — LTS are nicknames for NVM automatic maintenance, indicating the latest version and the latest long-term support version. System is also an alternate name for a non-NVM installed system version of Node

About existing Node modules

You can use — shots-Packs-from =node to automatically install the existing version of Node modules in the new node

nvm install node --reinstall-packages-from=node        Inherit from the previous version
nvm install 6 --reinstall-packages-from=5              # Inherit from a version
Copy the code

About the mirror

Mysteriously, NVM Windows provides the command line to set up the image, while MVN does not, so it has to be done manually.

##### NVM node image installation 1 In manual mode, perform this operation each time NVM installation is performed

export NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
nvm install node
Copy the code

Bashrc = ~/.bashrc = ~/.bashrc

The installation speed of ##### NPM image NPM install is also impressive. Therefore, after installing Node, the NPM image will also be set to Taobao.

npm config set registry https://registry.npm.taobao.org
Copy the code

Note: do not use CNPM, basically the same as NPM, but often some unexpected bugs.

uninstall

The uninstallation of NVM is done manually, but not complicated.

First, delete the NVM file

$ rm -rf "$NVM_DIR"
Copy the code

Next, fix the environment variable, modify ~/.bashrc (if not, look for bash_profile), and delete the following lines

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion
Copy the code

Get things done.

conclusion

MVN provides a convenient way to manage node.js versions, whether installing, switching, or deleting them in one step, and is undoubtedly the first choice at this stage.