nvm: Cannot call `nvm exec` in bash script with `set -e`

If I call nvm exec node foobar.js in a bash script with set -e everything exits early, this is not great for being able to use nvm in an automated build for example . . .

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 22 (11 by maintainers)

Commits related to this issue

Most upvoted comments

It appears nvm install <version> fails when set -e is used also. Here’s an example script I’m using:

#!/bin/sh
set -e   
[ -z "${DEBUG}" ] || set -x

green="$(tput setaf 2)"
reset="$(tput sgr0)"

export NVM_DIR="$HOME/.nvm"

NODE_VERSION="7.8"
BASEDIR="$(dirname "$0")/.."
cd "$(dirname "$0")/.."

SHORT_PIPELINE_VERSION=${PIPELINE_VERSION#*/}

if [ ! -d $NVM_DIR ]; then
    echo $green "\n==> Installing Node Version Manager" $reset
    (
        git clone -n https://github.com/creationix/nvm.git "$NVM_DIR"
        cd "$NVM_DIR"
        git checkout -q `git describe --abbrev=0 --tags --match "v[0-9]*" origin`
    )
fi

. $NVM_DIR/nvm.sh

echo $green "\n==> Installing node" $reset

nvm install $NODE_VERSION

The offending non-zero returning function is nvm_version_greater, called from nvm_version_path. Should I open a new ticket for this?

@kncs thanks - i was able to find the line causing the error. Definitely every file ever should have a trailing newline, but, I’ll figure out a way to make this not break.

I don’t know if it could help someone but I just solve a similar problem using set -e and nvm into a bash script. The problem was a missing line break into the .nvmrc file.

How to reproduce it :

  1. Create a bash file
#!/bin/bash
set -e
source ~/.nvm/nvm.sh
nvm --version 
nvm use 
  1. Remove line break into the .nvmrc

  2. Launch script and observe.

@joscha when the version is omitted and no .nvmrc is present, it does fail (and should) - is that the situation for you?