[英]Can't use nvm from bash script
I am trying to write a shell script to automate my dev environment set-up (install python, nvm, node, mongo etc...).我正在尝试编写一个 shell 脚本来自动化我的开发环境设置(安装 python、nvm、node、mongo 等......)。 I am using nvm to install Node.我正在使用 nvm 安装 Node.js。 It tells you to close and reopen your terminal to start using the nmv command.它告诉您关闭并重新打开终端以开始使用 nmv 命令。 I tried to source .bashrc and .profile to make the command available right away so I can continue running the script with nvm install, but it doesn't work.我尝试获取 .bashrc 和 .profile 以立即使命令可用,以便我可以继续使用 nvm install 运行脚本,但它不起作用。
Here is the segment of my script related to installing NVM / Node:这是我的脚本中与安装 NVM/Node 相关的部分:
#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
I get these messages, but please note that I've already run the script and NVM / Node are already installed and working.我收到了这些消息,但请注意,我已经运行了脚本并且 NVM/Node 已经安装并可以工作。 I can also use nvm and node in the same terminal I run the script from after it completes.我还可以在脚本完成后在运行脚本的同一个终端中使用 nvm 和 node。 It just doesn't work in the script.它只是在脚本中不起作用。
=> 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
if you have nvm running on the main shell, you just need to add:如果您在主 shell 上运行 nvm,您只需要添加:
export NVM_DIR=$HOME/.nvm;
source $NVM_DIR/nvm.sh;
in your script在你的脚本中
Here is what worked for me.这对我有用。
First install nvm (once and separately) using SSH or console:首先使用 SSH 或控制台安装 nvm(一次且单独安装):
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
Then in your script, loading your profile as follows:然后在您的脚本中,按如下方式加载您的配置文件:
. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc
And with some luck nvm
should become available inside the script.幸运的是, nvm
应该可以在脚本中使用。
nvm install 4.4.2
Tada!多田!
Just put this on top of your script:只需将其放在您的脚本之上:
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
Worked like a charm here.在这里工作就像一个魅力。
This script works fine for me:这个脚本对我来说很好用:
#!/usr/bin/env bash
if [ ! -d ~/.nvm ]; then
curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
source ~/.nvm/nvm.sh
source ~/.profile
source ~/.bashrc
nvm install 5.0
npm install
npm run front
fi
Nowadays, you can simply do this:如今,您可以简单地执行以下操作:
env NODE_VERSION=<dd> /home/<user>/.nvm/nvm-exec npm run front
Simply sourcing nvm.sh didn't work for me (from a systemd .service file), the PATH didn't include ~/.nvm...
简单地采购 nvm.sh 对我不起作用(来自 systemd .service 文件),PATH 不包括~/.nvm...
Credit where credit is due: https://gist.github.com/joepie91/73ce30dd258296bd24af23e9c5f761aa#gistcomment-2215867信用到期的信用: https : //gist.github.com/joepie91/73ce30dd258296bd24af23e9c5f761aa#gistcomment-2215867
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.