node.js nvm 在新的终端会话中保持“忘记”节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24585261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:36:42  来源:igfitidea点击:

nvm keeps "forgetting" node in new terminal session

node.jsmacosnpmhomebrewnvm

提问by frhd

Upon using a new terminal session in OS X, nvmforgets the node version and defaults to nothing:

在 OS X 中使用新的终端会话时,nvm忘记节点版本并默认为空:

$ nvm ls:

$ nvm ls

         .nvm
     v0.11.12
     v0.11.13

I have to keep hitting nvm use v.0.11.13in every session:

我必须nvm use v.0.11.13在每个会话中继续击球:

         .nvm
     v0.11.12
->   v0.11.13

I've tried both the brewinstall, as well as the official installation script.

我已经尝试了brew安装以及官方安装脚本。

My .profilefor 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

$ 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?

任何线索我做错了什么?

回答by dylants

Try nvm alias default. For example:

试试nvm alias default。例如:

$ nvm alias default 0.12.7

$ nvm alias default 0.12.7

This sets the default node version in your shell. Then verify that the change persists by closing the shell window, opening a new one, then: node --version

这会在您的 shell 中设置默认节点版本。然后通过关闭 shell 窗口,打开一个新窗口来验证更改是否仍然存在,然后: node --version

回答by Tarun

Alias to nodeitself to avoid updating the default alias along with node version updates later on.

node自己的别名以避免在以后更新默认别名以及节点版本更新。

nvm alias default node

回答by ohho

To install the latest stable version:

要安装最新的稳定版本:

nvm install stable

To set default to the stable version (instead of a specific version):

将默认设置为稳定版本(而不是特定版本):

nvm alias default stable

To list installed versions:

列出已安装的版本:

nvm list

As of v6.2.0, it will look something like:

v6.2.0,它看起来像:

$ nvm list
         v4.4.2
->       v6.2.0
default -> stable (-> v6.2.0)
node -> stable (-> v6.2.0) (default)
stable -> 6.2 (-> v6.2.0) (default)
iojs -> N/A (default)

回答by joeytwiddle

In my case, another program had added PATHchanges to .bashrc

就我而言,另一个程序已PATH.bashrc

If the other program changed the PATHafternvm's initialisation, then nvm's PATHchanges would be forgotten, and we would get the system node on our PATH(or no node).

如果其他程序PATHnvm 初始化更改了nvm 的PATH更改,那么 nvm 的更改将被遗忘,我们将在我们的PATH(或没有节点)上获取系统节点。

The solution was to move the nvm setup to the bottomof .bashrc

解决方案是将 nvm 设置移动到底.bashrc

### BAD .bashrc ###

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

# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"

Solution:

解决方案:

### GOOD .bashrc ###

# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"

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

(This was with bash 4.2.46 on CentOS. It seems to me like a bug in bash, but I may be mistaken.)

(这是 CentOS 上的 bash 4.2.46。在我看来,这像是 bash 中的一个错误,但我可能误会了。)

回答by Jeff Mikels

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.

就我而言,nvm.sh 在 .bashrc 中被调用,但随后 PATH 变量在 .bash_profile 中得到更新,这导致我的会话在 nvm 节点之前找到系统节点。

回答by Joe

The top rated solutions didn't seem to work for me. My solution is below:

评分最高的解决方案似乎对我不起作用。我的解决方案如下:

  1. Uninstall nvm completely using homebrew:brew uninstall nvm
  2. Reinstall brew install nvm
  3. In Terminal, follow the steps below(these are also listed when installing nvm via homebrew):

    mkdir ~/.nvm cp $(brew --prefix nvm)/nvm-exec ~/.nvm/ export NVM_DIR=~/.nvm source $(brew --prefix nvm)/nvm.sh

  1. 使用自制软件完全卸载 nvm:brew uninstall nvm
  2. 重新安装 brew install nvm
  3. 在终端中,按照以下步骤操作(通过自制软件安装 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 的等效配置文件中。(再次取自 whats使用自制软件在 NVM 安装中列出)

回答by Gabbar

If you have tried everything still no luck you can try this :_

如果你已经尝试了一切仍然没有运气,你可以试试这个:_

1 -> Uninstall NVM

1 -> 卸载 NVM

rm -rf ~/.nvm

2 -> Remove npm dependencies by following this

2 -> 按照删除 npm 依赖项

3 -> Install NVM

3 ->安装 NVM

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

4 -> Set ~/.bash_profileconfiguration

4 -> 设置~/.bash_profile配置

Run sudo nano ~/.bash_profile

sudo nano ~/.bash_profile

Copy and paste following this

复制并粘贴在此之后

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

5 -> CONTROL + Xsave the changes

5 ->CONTROL + X保存更改

6 -> Run . ~/.bash_profile

6 -> 运行 . ~/.bash_profile

7 -> Now you should have nvminstalled on your machine, to install node run nvm install v7.8.0this will be default node version or you can install any version of node

7 -> 现在你应该已经nvm在你的机器上安装了,安装 node 运行nvm install v7.8.0这将是默认的 node 版本或者你可以安装任何版本的 node

回答by souparno majumder

This question has mentioned for the OSX, but it happened to me in my linux OS. I tried using nvm alias default <version>but for each new terminal session the used node version was forgotten. so, here is the solution that i figured out.

这个问题在 OSX 中提到过,但它发生在我的 linux 操作系统中。我尝试使用,nvm alias default <version>但是对于每个新的终端会话,使用的节点版本都被忘记了。所以,这是我想出的解决方案。

make sure to set a default alias for node version,put the following code in .bashrc, and source .bashrc.

确保为节点版本设置默认别名,将以下代码放入 .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

descriptive solution link

描述性解决方案链接

回答by Denis Howe

Doing nvm install 10.14, for example, will nvm usethat version for the current shell session but it will notalways set it as the default for future sessions as you would expect. The node version you get in a new shell session is determined by nvm alias default. Confusingly, nvm installwill only set the default alias if it is not already set. To get the expected behaviour, do this:

这样做nvm install 10.14,例如,将nvm use那个版本当前shell会话,但它并不总是被设置为默认为未来的会议如你所愿。您在新 shell 会话中获得的节点版本由nvm alias default. 令人困惑的是,nvm install如果尚未设置,则只会设置默认别名。要获得预期的行为,请执行以下操作:

nvm alias default ''; nvm install 10.14

This will ensure that that version is downloaded, use it for the current session andset it as the default for future sessions.

这将确保下载该版本,将其用于当前会话并将其设置为未来会话的默认值。

回答by Harshit Garg

I have found a new way here. Using nInteractively Manage Your Node.js helps.

我在这里找到了一种新方法。使用n交互式管理您的 Node.js 会有所帮助。