本文翻译自:nvm keeps “forgetting” node in new terminal session
Upon using a new terminal session in OS X, nvm
forgets the node version and defaults to nothing: 在OS X中使用新的终端会话时, nvm
忘记节点版本并默认为nvm
:
$ nvm ls
: $ nvm ls
:
.nvm
v0.11.12
v0.11.13
I have to keep hitting nvm use v.0.11.13
in every session: 我必须在每个会话中继续nvm use v.0.11.13
:
.nvm
v0.11.12
-> v0.11.13
I've tried both the brew
install, as well as the official installation script. 我已经尝试了brew
安装,以及官方安装脚本。
My .profile
for the brew version: 我的.profile
为brew版本:
#nvm
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
And for the install.sh script: 对于install.sh脚本:
$ curl https://raw.githubusercontent.com/creationix/nvm/v0.10.0/install.sh | bash
#nvm
export NVM_DIR="/Users/farhad/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Any clue to what I'm doing wrong? 我有什么不对的任何线索?
#1楼
参考:https://stackoom.com/question/1f9kT/nvm在新的终端会话中保持-遗忘-节点
#2楼
Try nvm alias default
. 尝试使用nvm alias default
。 For example: 例如:
$ nvm alias default 0.12.7
This sets the default node version in your shell. 这将在shell中设置默认节点版本。 Then verify that the change persists by closing the shell window, opening a new one, then: node --version
然后通过关闭shell窗口,打开一个新窗口,然后: node --version
验证更改是否仍然存在
#3楼
nvm does its job by changing the PATH variable, so you need to make sure you aren't somehow changing your PATH to something else after sourcing the nvm.sh script. nvm通过更改PATH变量来完成它的工作,所以你需要确保在获取nvm.sh脚本后你不会以某种方式将PATH更改为其他东西。
In my case, nvm.sh was being called in .bashrc but then the PATH variable was getting updated in .bash_profile which caused my session to find the system node before the nvm node. 在我的例子中,在.bashrc中调用了nvm.sh,然后在.bash_profile中更新了PATH变量,导致我的会话在nvm节点之前找到了系统节点。
#4楼
别名到node
本身以避免更新默认别名以及稍后的节点版本更新。
nvm alias default node
#5楼
The top rated solutions didn't seem to work for me. 最受好评的解决方案似乎对我不起作用。 My solution is below: 我的解决方案如下:
- Uninstall nvm completely using homebrew:
brew uninstall nvm
使用homebrew:brew uninstall nvm
完全brew uninstall nvm
- Reinstall
brew install nvm
重新安装brew install nvm
In Terminal, follow the steps below(these are also listed when installing nvm via homebrew): 在终端中,请按照以下步骤(通过自制软件安装nvm时也会列出这些步骤):
mkdir ~/.nvm cp $(brew --prefix nvm)/nvm-exec ~/.nvm/ export NVM_DIR=~/.nvm source $(brew --prefix nvm)/nvm.sh
The steps outlined above will add NVM's working directory to your $HOME path, copy nvm-exec to NVM's working directory and add to $HOME/.bashrc, $HOME/.zshrc, or your shell's equivalent configuration file.(again taken from whats listed on an NVM install using homebrew) 上面列出的步骤将NVM的工作目录添加到$ HOME路径,将nvm-exec复制到NVM的工作目录并添加到$ HOME / .bashrc,$ HOME / .zshrc或shell的等效配置文件。(再次从什么中获取)在使用自制软件的NVM安装中列出)
#6楼
This question has mentioned for the OSX, but it happened to me in my linux OS. 这个问题已经提到了OSX,但它发生在我的Linux操作系统中。 I tried using nvm alias default <version>
but for each new terminal session the used node version was forgotten. 我尝试使用nvm alias default <version>
但是对于每个新的终端会话,忘记了使用的节点版本。 so, here is the solution that i figured out. 所以,这是我想出的解决方案。
make sure to set a default alias for node version ,put the following code in .bashrc, and source .bashrc
. 确保为节点版本设置默认别名 ,将以下代码放在.bashrc中,并将source .bashrc
代码放在source .bashrc
。
export NVM_DIR="/home/bonnie/.nvm"
## If the file exists and is not empty
if [ -s "$NVM_DIR/nvm.sh" ]; then
## Source it
source "$NVM_DIR/nvm.sh"
fi
NODE_DEFAULT_VERSION=$(<"$NVM_DIR/alias/default")
export PATH="$NVM_DIR/versions/node/$NODE_DEFAULT_VERSION/bin":$PATH