Use nvm to manage nodes, npm, and nvmnpm of different versions

Source: Internet
Author: User

Use nvm to manage nodes, npm, and nvmnpm of different versions

Note: nvm installed by brew install nvm on Mac cannot be correctly enabled due to different installation paths. We recommend that you use brew uninstall nvm to uninstall it and reinstall it once through the solution described in this article.

---

Recently, NodeJS version updates are somewhat faster than Chrome. The version 4.0 was not released for a long time, and another version 5.0 was launched. After NodeJS is upgraded, you can easily start using some ES6 language features, but it will make the internal mz-Fs framework of the team unable to be updated, because it only supports v0.12 for the time being. As a result, the team started to try nvm, a tool for managing node versions. It was found that nvm is easy to use on Mac. We recommend that you use it as soon as possible.

Nvm is a node management tool in Mac. It is a bit similar to rvm in Ruby. If you need to manage nodes in Windows, nvmw or nvm-windows is recommended.

The following describes how to install and use the Mac system (Windows is for reference only ).

 

1. Uninstall the node/npm that has been installed to the global environment

If you have downloaded the node installation package from the official website, the package is automatically installed in the global directory.

The node command is in the/usr/local/bin/node directory, and the npm command is in the global node_modules directory. The specific path is/usr/local/lib/node_modules/npm.

After nvm is installed, it is best to delete the installed node and global node modules first:

Npm ls-g -- depth = 0 # Check the global modules that have been installed, to delete these global modules and then reinstall sudo rm-rf/usr/local/lib/node_modules according to different node versions # Delete the global node_modules directory
Sudo rm/usr/local/bin/node # delete a node
Cd/usr/local/bin & ls-l | grep ".. /lib/node_modules/"| awk '{print $9}' | xargs rm # Delete the soft chain registered by the global node Module

 

Ii. Install nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash

After the installation is complete, re-open the terminal environment. We recommend that you use oh-my-zsh in Mac instead of the default bash shell.

 

Iii. Install and switch between different versions of node/npm
Nvm install stable # install the latest stable version of node. Now it is 5.0.0nvm install 4.2.2 # install Version 4.2.2 nvm install 0.12.7 # install Version 0.12.7
# Note: the installation of the following modules is only for demonstration purposes. nvm use 0 is not required for installation of modules # Switch to version 0.12.7
Npm install-g mz-fiis # install the mz-fiis module to the global directory. The installation path is/Users/<your username> /. nvm/versions/node/v0.12.7/lib/mz-fiis
Nvm use 4 # Switch to version 4.2.2
Npm install-g react-native-cli # install the react-native-cli module to the global directory. The installation path is/Users/<your username> /. nvm/versions/node/v4.2.2/lib/react-native-cli

Nvm alias default 0.12.7 # Set the default node version to 0.12.7

 

4. Use the. nvmrc file to configure the node version used by the Project

If your default node version (set using the nvm alias command) is different from the version required by the project, you can create it in the project root directory or any parent directory. in the nvmrc file, specify the node version number used in the file, for example:

Cd <project root directory> # Go To The project root directory echo 4>. nvmrc # Add. nvmrc file nvm use # It is automatically used without specifying a version number. node-v configured in the nvmrc file # Check whether the node is switched to the corresponding version

 

V. Differences between nvm and n

The node version management tool has another n command, which is a node module and nvm is an external shell script independent of node/npm, therefore, n commands are more limited than nvm commands.

Since the npm installation module paths are/usr/local/lib/node_modules, when using n to switch different node versions, the global node/npm directory will actually be shared. Therefore, it cannot meet the requirements of "using different global node modules based on different node versions.

 

Therefore, we recommend that you use nvm as soon as possible to avoid the problem that the global module cannot be updated.

Of course, if you are using a windows operating system, I can only say ...... Friend, can you change your Mac to a tasteful programmer? P

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.