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均安装成功。若出现问题,请回顾前面内容。

猜你喜欢

转载自blog.csdn.net/u011608531/article/details/106885874