1. 安裝nvm
- 為了兼容性和效率伍掀,nvm和n進行Node版本管理很有必要掰茶,我選擇使用nvm控制node版本。首先要說的是不能用brew去安裝nvm蜜笤,官方文檔已經(jīng)說明不支持濒蒋,這里用以下命令安裝:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
- 此時nvm就被安裝在了
~/.nvm
下啦,接下來就需要配一下環(huán)境變量了把兔,這里以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都會被自動添加到環(huán)境變量中了。
- 完成后輸入
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
試試,不過這里推薦使用國內(nèi)鏡像:
NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node nvm install 6
需要哪個版本鳍置,就在最后將6換成對應(yīng)的版本號就行辽剧,默認下載版本中最新的版本號。
3. 切換Node版本
沒有嘗試税产,目前還沒有需求怕轿,具體的可以參考如下鏈接:
使用nvm輕松切換node版本
轉(zhuǎn)載注明出處哦,謝謝啦