mac 安裝Node和NPM

一、安裝環境準備

1. 安裝nvm

sh-3.2# curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
sh-3.2# sh-3.2# source ~/.bash_profile 

2. 查看哪些版本可以安裝

備註:不知道爲什麼,速度挺慢的

sh-3.2# nvm ls-remote
         v9.2.0
         v9.2.1
         v9.3.0
         v9.4.0
         v9.5.0

3. 安裝node及npm

備註:不知道爲什麼,速度很慢的

sh-3.2# nvm install v9.5.0

4. 將鏡像源設置爲淘寶鏡像

export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
npm --registry=https://registry.npm.taobao.org

5. 安裝brew

# 切換爲非root用戶執行此步驟
sh-3.2# su lizhen7
bash-3.2$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

二、brew 安裝node

8. 首先更新brew,使其在最新版本

代碼如下:

brew update

9. 確保brew是安全可靠的

代碼如下:

$ brew doctor

將可能導致如下情況,可針對性逐條處理,處理完成放可完成下一步:

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

10. brew環境變量配置

將brew的位置添加到$PATH環境變量中,並保存bash或者profile文件;

export PATH="/usr/local/bin:$PATH"

11. 當處理完上述問題後,來處理brew和node關係

若在上文中出現,如下錯誤信息:

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. Run brew 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有關的目錄;
  • 2.在/usr/local/include 目錄下,刪除任何與node 和 node_modules有關的目錄;
  • 3.如果你是通過brew install node安裝的node,則在終端執行brew uninstall node ,並在home目錄下查找 locallibinclude文件夾,刪除任何與nodenode_modules有關的目錄;
  • 4.在**/usr/local/bin**目錄下,刪除任何與 node 執行文件;
  • 5.最後下載 nvm ,跟隨它的介紹安裝node。當然,你也可以通過npm來安裝最新版本的Node。

12. 通過brew安裝node和npm

brew link node
brew uninstall node
brew install node

13. 測試Node和npm安裝是否成功,安裝Grunt

npm install -g grunt-cli

如果安裝成功,那麼恭喜你node,npm,grunt均安裝成功。若出現問題,請回顧前面內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章