preface
At present, there are two mainstream Node version management tools, NVM and N. The difference between the two is quite large, specific analysis can refer to an article of Taobao FED team:
To manage node versions, choose NVM or N?
In general, NVM is similar to Python’s VirtualEnv or Ruby’s RVM, in that each node version of the module is sandboxed (so modules need to be reinstalled after switching versions). Therefore, considering the need to frequently switch node versions to test compatibility and the limitations of some modules on Node versions, I chose to use NVM as the management tool. Here is the installation and use process of NVM.
The installation
This section mainly introduces the installation of MAC (Linux can refer to), Windows system can refer to Windows
This method is not recommended
I used Homebrew to install it initially, using the following installation commands:
brew install nvmCopy the code
Once installed, to allow you to use NVM commands directly from your shell, You must add the following line to your.bash_profile (people who like to put their Settings in.bashrc can change the following.bash_profile to.bashrc).
source $(brew --prefix nvm)/nvm.shCopy the code
Or simply enter the line below to join
$ echo "source $(brew --prefix nvm)/nvm.sh" >> .bash_profileCopy the code
Remember to re-source your.bash_profile to make it work
$ . ~/.bash_profileCopy the code
OK, that completes the NVM installation.
Let’s install Node V5.12.0
nvm install 5Copy the code
After the installation is successful, use NVM Use to switch versions
NVM use v5.12.0Copy the code
However, the error appears as follows:
NVM use v4.6.2Copy the code
And so it is:
nvm is not compatible with the npm config "prefix" option: Currently set to "/ Users/Fabian/NVM/versions/node/v0.12.7" Run ` NVM use -- delete - prefix v4.6.2 ` to unset it.Copy the code
So only install prompt, use
NVM use -- delete - prefix v4.6.2Copy the code
The switchover succeeds.
Please refer to an issue on Github as to why homebrew installs the problem above.
Homebrew Installation is not supported. (NVM Installation is not supported.)
Recommended installation method
Uninstall node/ NPM installed globally
If you downloaded the Node installation package from the official website, it will be automatically installed in the global directory
The node command is in /usr/local/bin/node, and the NPM command is in /usr/local/lib/node_modules/ NPM
After installing NVM, it is best to remove the installed Node and global Node modules first:
-g --depth=0 Sudo rm -rf /usr/local/lib/node_modules # delete the global node_modules directory sudo rm / usr/local/bin/node # delete node CD/usr/local/bin && ls -l | grep ".. / lib/node_modules/" | awk '{print $9}' | xargs rm # delete global registration of soft chain node moduleCopy the code
2. Install the NVM
The curl - o - https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bashCopy the code
After the installation is complete, restart the terminal environment. On a Mac, you are advised to use oh-my-zsh instead of the default bash shell. The NVM Install Stable is slow to install nodes. Add the environment variable NVM_NODEJS_ORG_MIRROR to the.bash_profile file, which I recommend:
# nvm
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/nodeCopy the code
You can then proceed with the easy installation of each version of Node by looking at the version you currently have installed:
$NVM ls NVM v0.8.26 v0.10.26 v0.11.11 -> v4.3.2Copy the code
3. Use CNPM to accelerate NPM
Similarly with NVM, NPM acquires and downloads package information from foreign sources by default. You can use the domestic image registry.npm.taobao.org with a simple registry parameter:
$ npm --registry=https://registry.npm.taobao.org install koaCopy the code
The screen banged again:
$ npm --registry=https://registry.npm.taobao.org install koa npm http GET https://registry.npm.taobao.org/koa npm http 200 https://registry.npm.taobao.org/koa ... npm http 200 https://registry.npm.taobao.org/negotiator npm http 200 https://registry.npm.taobao.org/keygrip Koa [@ 0] (/ user / 0). 5.2 node_modules/koa ├ ─ ─ koa - compose [2] @ (/ user / 2). 2.0 ├ ─ ─ statuses [1] @ (/ user / 1). 0.2 ├ ─ ─ Finished [1] @ (/ user / 1). 1.1 ├ ─ ─ the escape - HTML [1] @ (/ user / 1). 0.1 ├ ─ ─ only [@ 0] (/ user / 0). 0.2 ├ ─ ─ the debug [@ 0] (/ user / 0). 8.0 ├ ─ ─ Fresh [@ 0] (/ user / 0). 2.2 ├ ─ ─ type - is [1] @ (/ user / 1). 0.1 ├ ─ ─ delegates [@ 0] (/ user / 0). 0.3 ├ ─ ─ mime [1] @ (/ user / 1). 2.11 ├ ─ ─ Co [@ 3] (/ user / 3). 0.5 ├ ─ ─ accepts [1] @ (/ user / 1), 0.1 (negotiator [@ 0] (/ user / 0). 4.2) └ ─ ─ cookies [@ 0] (/ user / 0). 4.0 (keygrip [1] @ (/ user / 1). 0.0)Copy the code
After all, there is still a synchronization time difference between the image and the official NPM source. Currently, the default synchronization time interval for CNPM is 15 minutes. If you are a module publisher, or you want to synchronize a module immediately, then it is recommended that you install CNPM CLI:
$ npm --registry=https://registry.npm.taobao.org install cnpm -gCopy the code
From the CNPM command line, you can quickly synchronize any module:
$ cnpm sync koa connect mochaCopy the code
Well, I just don’t want to install CNPM CLI. Ha ha, I thought you would be so lazy, so we have a web page:
For example, if I want to synchronize KOA immediately, I go to the browser: npm.taobao.org/sync/koa
Or if you are a command-line user, use the open command:
$ open https://npm.taobao.org/sync/koaCopy the code
If you install a module that relies on C++ modules and needs to be compiled, you will be sure to compile it through node-gyp. When node-gyp is compiled for the first time, you will need to rely on node source code, so you will download node dist. Why NPM installation is so slow…
Miley Cyrus: See you again. See you again. See you again.
$ npm --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao.org/mirrors/node install microtimeCopy the code
Again, CNPM CLI, which has — Registry and — Miley Cyrus configured by default, knows if you don’t want to use CNPM CLI, you can
npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --globalCopy the code
Common NVM commands
-
NVM install <version> ## Install v4.4.0, NVM install v4.4.0, NVM install 4.4
-
NVM uninstall <version> ## Deletes the specified version that has been installed. The syntax is similar to install
-
NVM use <version> ## Switch to using the specified version of Node
-
NVM ls ## lists all installed versions
-
NVM ls-remote ## List all remote server versions
-
NVM current ## Displays the current version
-
NVM alias <name> <version> ## Add aliases for different version numbers
-
NVM unalias <name> ## Delete the defined alias
- NVM Scant-Packages
## Reinstall the specified version of the NPM package globally in the current Node version environment
Attached :MAC open.bash_profile
-
Start Terminal
- Go to the current user’s home directory
Type CD ~Copy the code
- Create the following.
Input touch. FollowingCopy the code
- Edit. Bash_profile file
Type open-e. bash_profileCopy the code
-
Save the file and close.bash_profile
- Update the newly configured environment variable
The input source. The followingCopy the code
The differences and relationships between profile, bashrC, and BASH_profile in Linux