"Close and reopen your terminal to start using nvm" 不工作

"Close and reopen your terminal to start using nvm" not working

我正在使用配备新 Apple M1 芯片的 MacBook Air,我正在尝试安装 nvm。 首先,我检查它是否已经安装:

MacBook-Air-gignu:~ gignu$ nvm -v
-bash: nvm: command not found

好像没有安装。所以我尝试像这样安装它:

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

现在它告诉我 nvm 已经安装:

=> nvm is already installed in /Users/gignu/.nvm, trying to update using git
=> => Compressing and cleaning up git repository

=> nvm source string already in /Users/gignu/.bash_profile
=> bash_completion source string already in /Users/gignu/.bash_profile
=> Close and reopen your terminal to start using nvm or run the following to use it now:

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

显然,nvm 已经安装,我所要做的就是“关闭并重新打开”我的终端。但是,如果我关闭终端,重新打开它并输入 nvm -v,我会得到与第一次相同的响应:command not found

到目前为止我尝试了什么

我已经删除了主目录中的.bashrc 文件并创建了一个包含以下内容的新文件:

export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"

没用。

我也做过同样的事情,只是内容不同:

NVM_DIR = "${XDG_CONFIG_HOME / - .$HOME /} NVM"
[-s"$NVM_DIR / nvm.sh"] && \. "$NVM_DIR / nvm.sh"#This will load nvm

还有这个:

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

无论我将其放入 .bashrc 文件还是 .bash_profile 文件,都不起作用。

暂时有效的是在终端中执行上面的代码:

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

现在,当我检查 nvm 时,它已安装:

MacBook-Air-gignu:~ gignu$ nvm -v
0.37.2

但是如果我关闭终端然后重新打开它,nvm 就不会再安装了。

提前感谢您的帮助!

我找到了解决方案!

您必须将以下内容放在 .bashrc 文件或 .bash_profile 文件的开头:

source ~/.bashrc

我的 .bash_profile 文件现在看起来像这样:

source ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion