【原创】MacOS 使用 brew 安装Node.js

安装环境

MacOS 10.15.4

$ sw_vers          
ProductName:    Mac OS X
ProductVersion: 10.15.4
BuildVersion:   19E287

home-brew 

$ brew -v      
Homebrew 2.2.11
Homebrew/homebrew-core (git revision 0ce758; last commit 2020-03-26)
Homebrew/homebrew-cask (no git repository)

安装准备

1、Node.js 是什么

As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications

 

官网的说明:Node.js 是一个异步的,事件驱动的 JavaScript 的运行时环境,它用于构建可扩展的网络应用程序。当然,它本身也是一门后端的编程语言。

更多的关于 Node.js 的信息可参考官方网站的介绍。

2、Node.js 官网

中文

https://nodejs.org/zh-cn/

英文

https://nodejs.org/en/

安装步骤

1、安装 Node.js

直接使用 brew install node 命令安装即可。

$ brew install node

$ node -v                                 
v13.5.0
$ npm -v 
6.13.4

2、安装 n 模块

n 模块用于方便切换 Node 版本

$ npm i -g n

n 模块切换 node 版本常用命令:

n stable 切换到稳定版本
n lts 切换到最新的 LTS 版本
n latest 切换到最新的 node 版本
n x.x.x 切换到某个指定的 node 版本

注: 

a. 如果执行 n 命令时提示权限不足,请加上 sudo。或者保险的做法是切换时命令前都加上 sudo

b. n 模块只支持 MacOS 与 Linux 系统,不支持 Windows 系统。Windows 系统切换版本请自行安装

示例:

我当前使用的是最新的 LTS 版本即 13.5.0。下面是我从 LTS 版本切换到 stable 版本的示例:

$ node -v                                 
v13.5.0
$ sudo n stable
Password:
  installing : node-v12.16.3
       mkdir : /usr/local/n/versions/node/12.16.3
       fetch : https://nodejs.org/dist/v12.16.3/node-v12.16.3-darwin-x64.tar.gz
-=O=-                                                                                                                                                #      #    #   ##-=O=-                                                                                                                                                        #    #  ##-=O=-                                                                                                                                                          #    #-=O=-                                                                                                                                                            #   ##-=O=-                                                                                                                                                              # ##-=O=-                                                                                                                                                              #-=O=-                                                                                                                                                             #  ##-=O=-                                                                                                                                                           #   #-=O=-                                                                                                                                                       #    #   ##-=O=-                                                                                                                                                    #     #    #-=O=-                                                                                                                                                 #      #    #     installed : v12.16.3 (with npm 6.14.4)

n 模块官方说明

https://npmjs.com/package/n

相关配置

配置 npm 镜像源,可加速 npm 的下载

$ npm config set registry https://registry.npm.taobao.org --global
$ npm config set disturl https://npm.taobao.org/dist --global

使用下面的命令验证:

$ npm config list                                            
; cli configs
metrics-registry = "https://registry.npm.taobao.org/"
scope = ""
user-agent = "npm/6.14.4 node/v12.16.3 darwin x64"
; globalconfig /usr/local/etc/npmrc
disturl = "https://npm.taobao.org/dist"
registry = "https://registry.npm.taobao.org/"
; node bin location = /usr/local/bin/node
; cwd = /Users/jiangzhuolin
; HOME = /Users/jiangzhuolin
; "npm config ls -l" to show all defaults.

npm config 常用命令:

Usage:
npm config set <key> <value>    # 设置某一项配置项的值,key 为配置项名称,value 是配置值
npm config get [<key>]    # 获取某一项指定的配置项的值
npm config delete <key>    # 删除某一项指定的配置项的值
npm config list [--json]    # 列举当前的所有配置信息
npm config edit    # 通过 vim 模式编辑修复 npm 配置项
alias: c    # 别名,可以将上面命令中的 config 更换为 c,如果 npm c list --json

You may also like...

发表评论

电子邮件地址不会被公开。