Mac上安装Node和NPM【转】

时间:2022-02-28 13:23:06

http://www.jianshu.com/p/20ea93641bda

作为前端开发者,node和npm安装必不可少。然而有时会因为安装新的app(如MacPorts,慎装,它会修改基本环境变量以及npm的全局设置等)导致版本环境混乱,特别推荐安装brew来对node和npm版本进行管理。

安装brew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew安装node

  1. 首先更新brew,使其在最新版本,代码如下:
    $ brew update
  2. 确保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

  3. 将brew的位置添加到$PATH环境变量中,并保存bash或者profile文件;
    export PATH="/usr/local/bin:$PATH"

  4. 当处理完上述问题后,来处理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目录下查找 **local** 或**lib** 或 **include**文件夹,删除任何与**node** 和 **node_modules**有关的目录;
      4.在**/usr/local/bin**目录下,删除任何与 **node** 执行文件;
      5.最后下载 **nvm** ,跟随它的介绍安装node。当然,你也可以通过**npm**来安装最新版本的Node。
  5. 通过brew安装node和npm

    brew link node
    brew uninstall node
    brew install node
  6. 测试Node和npm安装是否成功,安装Grunt
    npm install -g grunt-cli
    如果安装成功,那么恭喜你node,npm,grunt均安装成功。若出现问题,请回顾前面内容。
文/阿啦嘞(简书作者)
原文链接:http://www.jianshu.com/p/20ea93641bda
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。