作為前端開發(fā)者译暂,node和npm安裝必不可少胃夏。然而有時(shí)會(huì)因?yàn)榘惭b新的app(如MacPorts蹬铺,慎裝,它會(huì)修改基本環(huán)境變量以及npm的全局設(shè)置等)導(dǎo)致版本環(huán)境混亂匈庭,有2種方式有效管理:
- 特別推薦安裝安裝nvm來(lái)管理Node版本;
- 其次推薦brew來(lái)對(duì)node和npm版本進(jìn)行管理浑劳。
(首選)nvm管理Node
目前我更多的使用nvm來(lái)進(jìn)行node版本管理,它會(huì)安裝相應(yīng)版本的npm魔熏。
執(zhí)行如下命令:
- 安裝nvm
curl -L -o- [http://build.sankuai.com/nvm/install](http://build.sankuai.com/nvm/install) | bash
上述失敗了的話試試這個(gè):
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
上述再失敗了的話試試這個(gè):
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
nvm 常用命令
- nvm install xxx ???? //安裝xxx版本號(hào)
- nvm use xxx ?????? //設(shè)置使用xxx版本號(hào)
- nvm ls ???????? //所有已安裝的node版本號(hào)
- nvm alias default v10.16.0 //nvm設(shè)置默認(rèn)node版本號(hào)
- 安裝node及npm
nvm install v8.5.0
- 將鏡像源設(shè)置為淘寶鏡像
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
npm --registry=https://registry.npm.taobao.org
安裝brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew安裝node
- 首先更新brew衷咽,使其在最新版本蒜绽,代碼如下:
$ brew update
- 確保brew是安全可靠的,代碼如下:
$ brew doctor
將可能導(dǎo)致如下情況躲雅,可針對(duì)性逐條處理鼎姊,處理完成放可完成下一步:
Warning: Some directories in /usr/local/share/man aren't writable.
This can happen if you "sudo make install" software that isn't managed
by Homebrew. If a brew tries to add locale information to one of these
directories, then the install will fail during the link step.You should probably
sudo chown -R $(whoami)
them:
/usr/local/share/man/man5
/usr/local/share/man/man7
- 將brew的位置添加到
PATH"`
- 當(dāng)處理完上述問題后相寇,來(lái)處理brew和node關(guān)系
若在上文中出現(xiàn),如下錯(cuò)誤信息:
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Runbrew link
on these:
node
則需要如下操作:
- 清理brew的link
$ brew cleanup
- 刪除node文件钮科,完全卸載node和npm
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
或者是
sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* /usr/local/lib/dtrace/node.d ~/.npm ~/.node-gyp /opt/local/bin/node opt/local/include/node /opt/local/lib/node_modules
或者是下面這樣:
1.在/usr/local/lib目錄下唤衫,刪除任何與node和 node_modules有關(guān)的目錄;
2.在/usr/local/include 目錄下绵脯,刪除任何與node 和 node_modules有關(guān)的目錄佳励;
3.如果你是通過**brew install node**安裝的node,則在終端執(zhí)行**brew uninstall node** 蛆挫,并在home目錄下查找 **local** 或**lib** 或 **include**文件夾赃承,刪除任何與**node** 和 **node_modules**有關(guān)的目錄;
4.在**/usr/local/bin**目錄下璃吧,刪除任何與 **node** 執(zhí)行文件楣导;
5.最后下載 **nvm** ,跟隨它的介紹安裝node畜挨。當(dāng)然筒繁,你也可以通過**npm**來(lái)安裝最新版本的Node。
- 通過brew安裝node和npm
brew link node
brew uninstall node
brew install node
- 測(cè)試Node和npm安裝是否成功巴元,安裝Grunt
npm install -g grunt-cli
如果安裝成功毡咏,那么恭喜你node,npm逮刨,grunt均安裝成功呕缭。若出現(xiàn)問題堵泽,請(qǐng)回顧前面內(nèi)容。