提问者:小点点

不能从bash脚本使用nvm


我正在尝试编写一个外壳脚本来自动化我的开发环境设置(安装python,nvm,node,mongo等…)。我正在使用nvm安装Node。它告诉您关闭并重新打开终端以开始使用nmv命令。我试图源. bashrc和.profile使命令立即可用,这样我就可以继续使用nvm install运行脚本,但它不起作用。

以下是我的脚本中与安装NVM/节点相关的部分:

#install nvm and latest node version
# sourcing profile and bashrc is not working here. nvm does not execute the next two lines to install node.

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
source ~/.profile
source ~/.bashrc
nvm install 5.0
nvm alias default node

我收到了这些消息,但请注意,我已经运行了脚本,NVM/节点已经安装并正常工作。我也可以在脚本完成后在运行脚本的同一终端中使用nvm和node。它在脚本中不起作用。

=> Downloading nvm from git to '/home/myDir/.nvm'
=> fatal: destination path '/home/myDir/.nvm' already exists and is not an empty directory.
fatal: Not a git repository (or any of the parent directories): .git
fatal: Not a git repository (or any of the parent directories): .git

=> Source string already in /home/myDir/.bashrc
=> Close and reopen your terminal to start using nvm
./install-programs.sh: line 27: nvm: command not found
./install-programs.sh: line 28: nvm: command not found

共3个答案

匿名用户

如果你有nvm在主shell上运行,你只需要添加:

export NVM_DIR=$HOME/.nvm;
source $NVM_DIR/nvm.sh;

在你的脚本

匿名用户

这是对我有用的。

首先使用SSH或控制台安装nvm(一次和单独):

$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash

然后在您的脚本中,按如下方式加载您的配置文件:

. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc

幸运的话,nvm应该可以在脚本中使用。

nvm install 4.4.2

多田!

匿名用户

只需将其放在您的脚本之上:

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

在这里工作得很有魅力。