大涛子客栈

问题描述: nvm 主要用于 Node 版本控制,macOS 系统下,在 zsh 或 bash 终端,使用 Homebrew 下载,一般都能正常使用,但是切换到 fish 终端就会无法找到此命令。

问题

在 fish 终端使用 nvm,会报找不到命令的错误,切换回 zsh,可以正常使用。

主要原因:fish 环境下无法识别 nvm shell 命令,比如 Gihub issues:NVM in fish:

May I request that this issue be reformatted in the form of a bug?

NVM does not install if you use the fish shell.
NVM does not work in the fish shell shell shell if previously installed in the bash shell. In fact, nvm > isn’t even in the path.

寻找解决办法

在刚刚 Github Issues 下就有解决方案:

I also wrote a general wrapper to bring almost any bash utility to fish shell: https://github.com/edc/bass. It works flawlessly with nvm using syntax like bass source ~/.nvm/nvm.sh \; nvm --version. For convenience, one can create an alias Fish function:

1
2
3
4
function nvm
bass source ~/.nvm/nvm.sh ';' nvm $argv
end
and then just use nvm --version, nvm ls, etc.

动手实践

第一步 下载 oh-my-fish

Oh My Fish provides core infrastructure to allow you to install packages which extend or modify the look of your shell. It’s fast, extensible and easy to use.

默认设置安装:

1
curl -L https://get.oh-my.fish | fish

检测:

1
2
omf -v
Oh My Fish version 7

或者可以自定义安装:

1
2
curl -L https://get.oh-my.fish > install
fish install --path=~/.local/share/omf --config=~/.config/omf

关于使用 curl,这里可能会报错:

1
Failed to connect to raw.githubusercontent.com port 443: Connection refused

说得也很明白,就是请求被拒绝访问,那我们怎么办?问度娘!咔咔咔,一段萌操作…之后:

访问ipaddress.com,获取 raw.githubusercontent.com 域名真实 IP,然后在本地 hosts 文件添加即可:

1
sudo vim /etc/hosts

添加如下内容,并保存退出:

1
199.232.28.133 raw.githubusercontent.com

第二步 下载 Bass

官网介绍:Bass makes it easy to use utilities written for Bash in fish shell.

下载好 oh-my-fish,直接下载 bass:

1
omf install bass

当然,也可以使用官网提到的 fisher、Fundle 等包管理器。

第三步 配置 nvm

前提是你已经下载好了 nvm,比如我使用 Homebrew 安装:

1
brew install nvm

然后进入如下目录,并创建 config.fish 文件:

1
2
cd ~/.config/fish/
touch config.fish

config.fish添加如下配置:

1
2
3
function nvm
bass source ~/.nvm/nvm.sh ';' nvm $argv
end

激活配置:

1
source ~/.config/fish/config.fish

最后检测:

1
2
nvm --version
0.34.0

大功告成,打完收工!

参考资料

 评论