Mac Nodejs安装与配置

nvm安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
% brew update
% brew install nvm
...
...
...
You should create NVM's working directory if it doesn't exist:

mkdir ~/.nvm

Add the following to ~/.zshrc or your desired shell
configuration file:

export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion

You can set $NVM_DIR to any location, but leaving it unchanged from
/usr/local/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.

Type `nvm help` for further information.

Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/nvm/0.35.3: 7 files, 150.0KB, built in 2 seconds

根据提示,创建 .nvm 目录

1
% mkdir ~/.nvm

编辑 ~/.zshrc 配置文件

1
% vi ~/.zshrc

在 ~/.zshrc 配置文件后添加如下内容

1
2
3
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"

:wq 保存并退出。

使用 source 命令使配置生效

1
% source ~/.zshrc

查看一下配置是否生效

1
2
% echo $NVM_DIR
/Users/your-username/.nvm

查看帮助

1
2
3
4
5
% nvm --help
...
...
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

帮助的最后一句话,可以看到删除 nvm 很简单,只要删除 $NVM_DIR 文件夹既可以

安装 node.js
查看 node.js 版本可以使用如下命令

1
% nvm ls-remote

安装

可以看到很多版本,接下来安装 LTS 版本

1
2
3
4
5
6
7
% nvm install 12
Downloading and installing node v12.18.3...
...
Computing checksum with shasum -a 256
Checksums matched!
Now using node v12.18.3 (npm v6.14.6)
Creating default alias: default -> 12 (-> v12.18.3)

安装完之后,可以查看一下

1
2
3
4
5
6
7
8
9
10
11
12
13
% nvm ls
-> v12.18.3
default -> 12 (-> v12.18.3)
node -> stable (-> v12.18.3) (default)
stable -> 12.18 (-> v12.18.3) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.18.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.22.0 (-> N/A)
lts/erbium -> v12.18.3

可以看到,安装的是 v12 的稳定版。因为只安装了一个版本,所以使用的版本是 v12

使用其他版本(建议安装稳定版本), 本例安装 v10 的稳定版为例

1
2
3
4
5
6
% nvm install 10
Downloading and installing node v10.22.0...
...
Computing checksum with shasum -a 256
Checksums matched!
Now using node v10.22.0 (npm v6.14.6)

可以看到当前 node 版本改为 v10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
% nvm ls
-> v10.22.0
v12.18.3
default -> 12 (-> v12.18.3)
node -> stable (-> v12.18.3) (default)
stable -> 12.18 (-> v12.18.3) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.18.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.22.0
lts/erbium -> v12.18.3

切换版本

1
2
% nvm use 12
Now using node v12.18.3 (npm v6.14.6)

切换默认的版本

1
2
nvm alias default 12.22.6

删除一个 node.js 版本

1
% nvm uninstall 10

Nodejs 卸载

brew的安装方式
直接通过一条命令

1
brew uninstall nodejs

官网下载pkg安装包的
通过这条命令

1
sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

参考资料