我正在尝试构建一个脚本,nvm
最终将在其中安装节点。
我已经安装了nvm
cURL。我看到.profile
或文件中的修改.bashrc
(都有效),并且在 bash 提示符下键入 时nvm
,它会显示可用的选项等。
所以 nvm 可以工作。我可以手动安装节点,但是一旦我将nvm
命令放入 shell 脚本中:
nano test.sh
#!/bin/bash
nvm
并运行它:
chmod 755 test.sh
./test.sh
我得到:
./test.sh: line 2: nvm: command not found
如果找不到nvm
,我什至不用去想
nvm ls-remote
或者
nvm install ...
我安装了 Ubuntu 14.04,Bash 是我的 shell。
答案1
nvm
command 是在~/.nvm/nvm.sh
.
您可以在开始时获取以下脚本之一以使其nvm()
可用:
. ~/.nvm/nvm.sh
. $(brew --prefix nvm)/nvm.sh # if installed via Brew
答案2
如果您在 OSX 上通过brew 安装了它,那么您可以通过获取有关安装的详细信息来将brew 源脚本加载到脚本环境中。
我在项目中有这个来引导它们:
brew install nvm
. $(brew --prefix nvm)/nvm.sh
nvm install
...
答案3
从这里和中的所有答案这个相关的 nvm 问题我创建这个脚本(要点)您可以复制或克隆(内联文档):
# shellcheck shell=sh
# https://gist.github.com/karfau/dcf98c6eefc2f2132c160f5c14d2112f
# needs to be sourced as part of your script
# 1. tries to configure nvm and run `nvm install`
# 2. checks if the node version is correct based on ./.nvmrc (`v` prefix not supported)
# if both doesn't work, exits with code 1 and some helpful messages
# https://unix.stackexchange.com/a/184512/194420
# https://github.com/nvm-sh/nvm/issues/1290
if [ -f ~/.nvm/nvm.sh ]; then
echo 'sourcing nvm from ~/.nvm'
. ~/.nvm/nvm.sh
elif command -v brew; then
# https://docs.brew.sh/Manpage#--prefix-formula
BREW_PREFIX=$(brew --prefix nvm)
if [ -f "$BREW_PREFIX/nvm.sh" ]; then
echo "sourcing nvm from brew ($BREW_PREFIX)"
. $BREW_PREFIX/nvm.sh
fi
fi
if command -v nvm ; then
nvm i
else
echo "WARN: not able to configure nvm"
fi
NODE_VERSION="v$(cat .nvmrc)"
which node
ACTIVE_VERSION=$(node --version)
GLOBAL_NPM=$(which npm || echo "not found on PATH")
# .nvmrc can contain only major or major.minor or full version
# so we replace active version with node version and anything afterwards
# if something is left, it's not a match
if [ "${ACTIVE_VERSION%%$NODE_VERSION*}" ] || [ ! -e "$GLOBAL_NPM" ]; then
echo "expected node '$NODE_VERSION' and npm on path"
echo "but was '$ACTIVE_VERSION' and npm:'$GLOBAL_NPM'"
exit 1
fi
希望它对要点有所帮助,欢迎反馈要点或在这里。
PS:与此同时,我更新了要点以使用NVM_DIR
环境变量而不是用户目录来支持自定义安装部分,添加了对调用nvm use
而不是nvm install
.
答案4
这个解决方案都没有解决我的问题,在使用 nvm 之前我必须在 sh 文件中执行此操作
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
nvm --version # this line can be delete