簡體   English   中英

無法從 bash 腳本使用 nvm

[英]Can't use nvm from bash script

我正在嘗試編寫一個 shell 腳本來自動化我的開發環境設置(安裝 python、nvm、node、mongo 等......)。 我正在使用 nvm 安裝 Node.js。 它告訴您關閉並重新打開終端以開始使用 nmv 命令。 我嘗試獲取 .bashrc 和 .profile 以立即使命令可用,以便我可以繼續使用 nvm install 運行腳本,但它不起作用。

這是我的腳本中與安裝 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

我收到了這些消息,但請注意,我已經運行了腳本並且 NVM/Node 已經安裝並可以工作。 我還可以在腳本完成后在運行腳本的同一個終端中使用 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

如果您在主 shell 上運行 nvm,您只需要添加:

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

在這里工作就像一個魅力。

這個腳本對我來說很好用:

#!/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

如今,您可以簡單地執行以下操作:

env NODE_VERSION=<dd> /home/<user>/.nvm/nvm-exec npm run front

簡單地采購 nvm.sh 對我不起作用(來自 systemd .service 文件),PATH 不包括~/.nvm...

信用到期的信用: https : //gist.github.com/joepie91/73ce30dd258296bd24af23e9c5f761aa#gistcomment-2215867

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM