You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac系统下Fish Shell无法运行nvm的解决方法咨询

问题

通过Brew安装Fish Shell后,按照指引将以下bash代码添加到~/.config/fish/config.fish和~/.zprofile中:

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

在普通终端(bash/zsh)中nvm可正常运行,但在Fish Shell中提示fish: Unknown command: nvm,执行source ~/.config/fish/config.fish时出现以下错误:

/opt/homebrew/Cellar/nvm/0.39.3/libexec/nvm.sh (line 386): Unexpected ')' found, expecting '}'
    *[!/]*/)
           ^
from sourcing file /opt/homebrew/Cellar/nvm/0.39.3/libexec/nvm.sh
    called on line 3 of file /opt/homebrew/opt/nvm/nvm.sh
from sourcing file /opt/homebrew/opt/nvm/nvm.sh
    called on line 6 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
.: Error while reading file '/opt/homebrew/Cellar/nvm/0.39.3/libexec/nvm.sh'
/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm (line 5): Missing end to balance this if statement
if ! command -v nvm &> /dev/null; then
^^
from sourcing file /opt/homebrew/opt/nvm/etc/bash_completion.d/nvm
    called on line 7 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
.: Error while reading file '/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm'

需要解决如何在Fish Shell中正常运行nvm的问题。

解决方案

错误原因

nvm的启动脚本nvm.sh和补全脚本是Bash专属语法,Fish Shell的语法规则与Bash不兼容,直接source这些脚本会触发语法错误,导致nvm无法加载。

方法一:使用bass工具兼容Bash脚本

bass是一个Fish工具,能在Fish环境中运行Bash脚本并同步环境变量:

  1. 先安装bass:
brew install bass
  1. 删除~/.config/fish/config.fish中之前添加的三行Bash代码,替换为:
set -x NVM_DIR "$HOME/.nvm"
bass source /opt/homebrew/opt/nvm/nvm.sh --no-use
  1. 重启Fish Shell,即可正常使用nvm命令。

方法二:安装Fish专用的nvm插件

使用Fish包管理器安装专为Fish适配的nvm插件,无需依赖Bash脚本:

  1. 若未安装Fish包管理器fisher,通过Brew安装:
brew install fisher
  1. 安装nvm插件:
fisher install jorgebucaran/nvm.fish
  1. 重启Fish Shell后,直接使用nvm命令即可,无需额外配置。

内容的提问来源于stack exchange,提问作者Pikk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 21:30:53