转载:https://www.jianshu.com/p/a3f8778bc0a1
https://segmentfault.com/a/1190000004404505
1. 安装nvm
- 为了兼容性和效率,nvm和n进行Node版本管理很有必要,我选择使用nvm控制node版本。首先要说的是不能用brew去安装nvm,官方文档已经说明不支持,这里用以下命令安装:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
- 此时nvm就被安装在了/.nvm下啦,接下来就需要配一下环境变量了,这里以zsh为例,在/.zshrc这个配置文件中配置,如果你的shell用的是bash或其它的,就找找看/.bash_profile或者/.profile,~/.bashrc。
- 编辑器打开
~/.zshrc
文件,如果不会就输入open ~/.zshrc
; - 在最下面一行输入:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
这一步的作用是每次新打开一个bash,nvm都会被自动添加到环境变量中了。
- 完成后输入
source ~/.zshrc
重新启动一下配置。 - 输入
nvm
可以看到如下信息:
zhao@EchodeMacBook-Pro ~ source ~/.zshrc
zhao@EchodeMacBook-Pro ~ nvm
Node Version Manager
Note: <version> refers to any version-like string nvm understands. This includes:
full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
default (built-in) aliases: node, stable, unstable, iojs, system
custom aliases you define with `nvm alias foo`
Usage:
nvm help Show this message
nvm --version Print out the latest released version of nvm
nvm install [-s] <version> Download and install a <version>, [-s] from source. Uses .nvmrc if available
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
nvm uninstall <version> Uninstall a version
nvm use [--silent] <version> Modify PATH to use <version>. Uses .nvmrc if available
nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available
nvm run [--silent] <version> [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available
nvm current Display currently activated version
nvm ls List installed versions
nvm ls <version> List versions matching a given description
nvm ls-remote List remote versions available for install
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
nvm deactivate Undo effects of `nvm` on current shell
nvm alias [<pattern>] Show all aliases beginning with <pattern>
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version
nvm unload Unload `nvm` from shell
nvm which [<version>] Display path to installed node version. Uses .nvmrc if available
Example:
nvm install v0.10.32 Install a specific version number
nvm use 0.10 Use the latest available 0.10.x release
nvm run 0.10.32 app.js Run app.js using node v0.10.32
nvm exec 0.10.32 node app.js Run `node app.js` with the PATH pointing to node v0.10.32
nvm alias default 0.10.32 Set default node version on a shell
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
至此,nvm安装完成。
2. 安装Node
- 查看Node所有版本,用
nvm ls-remote
命令,如下:
zhao@EchodeMacBook-Pro ~ nvm ls-remote
v0.1.14
v0.1.15
v0.1.16
...
v7.7.1
v7.7.2
v7.7.3
正常安装的话,会超级慢,如果想尝试,就用nvm install v6.10.0
试试,不过这里推荐使用国内镜像:
NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node nvm install 6
需要哪个版本,就在最后将6换成对应的版本号就行,默认下载版本中最新的版本号。
3. 切换Node版本
接下来我们可以使用 nvm use <version> 切换版本:
➜ ~ nvm use v4.1.0
Now using node v4.1.0 (npm v2.14.3)
➜ ~ node -v
v4.1.0
➜ ~ nvm use v5.5.0
Now using node v5.5.0 (npm v3.3.12)
➜ ~ node -v
v5.5.0
轻轻松松完成切换!
不过问题来了,这时如果你新打开一个bash,输入nvm current会发现显示为null,这是因为使用nvm use命令只会在当前bash里生效,重新打开一个bash你会发现$PATH的值已经不包含刚才的node目录了,要解决这个问题也很简单,使用nvm alias default <version>命令来指定一个默认的node版本就ok了,这里我使用了v4.1.0作为默认版本:
➜ ~ nvm alias default v4.1.0
default -> v4.1.0
此时再打开一个bash输入nvm current就会显示为v4.1.0了。