mac安装nvm遇到的问题小记(2021.02.07)

883 阅读2分钟

背景

最近换了新的mac(系统版本 macOS Catalina 10.15.7),需要重新配置开发环境,安装node版本控制工具nvm遇到的问题。
网上检索到的的中文解决方案,都是基于bash的有点过时了。
而自从macOS来到 Catalina 10.15 的默认Shell已经变成zsh了,所以我们直接用官方给的解决方法,详情见下文。

1、安装

直接去github nvm-sh/nvm 查看README.md,我这里选择cURL安装的方式(你也可以选择其它方式):

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

安装完成:

2、发现问题

运行nvm命令,这个时候会发现报错:

-bash: nvm: command not found

3、找到答案

继续看文档,最终在 Troubleshooting on macOS 中找到了答案:

Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default. Create one with touch ~/.zshrc and run the install script again.

原来从macOS 10.15之后,mac默认的shell从bash变成了zsh,nvm回去找.zshrc文件更新配置,但是找不到。解决方法就是新建一个.zshrc文件,再重新安装nvm即可

touch ~/.zshrc

再重新执行前面的安装命令:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

这个时候再执行nvm命令,已经成功了。这时候去查看~/.zshrc文件可以发现已经更新内容了,接下来就可以愉快的使用nvm啦!

4、nvm常用命令


nvm install 版本号 // 下载node版本

nvm use 版本号 // 切换node版本

nvm ls // 罗列本地下载的版本

nvm ls-remote // 罗列远程版本

5、新的问题

本来以为已经结束了,后面发现每次打开控制台都会出现异常提示:

zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]?

// 根据提示,运行compaudit命令
% compaudit 
There are insecure directories:
/usr/local/share/zsh/site-functions
/usr/local/share/zsh

在stackoverflow上找到了解决方法zsh compinit: insecure directories ,zsh的文件写入权限需要关闭,命令如下:

% chmod g-w /usr/local/share/zsh 
% chmod g-w /usr/local/share/zsh/site-functions 

上面链接可能需要翻墙,我直接贴一下截图:

结语

上面遇到的两个问题,还是花了点时间的,主要是基本都是在和英文打交道,而我的英文又不太好= =。
基于开源与分享的精神,在此分享给大家,希望对遇到类似的问题的同学有所帮助,谢谢!