Nvm: `nvm install` fails from a bash script

Created on 19 Jul 2019  ·  8Comments  ·  Source: nvm-sh/nvm

  • Operating system and version:

macOS Mojave 10.14.5

  • nvm debug output:


    nvm --version: v0.33.1
    $SHELL: /bin/bash
    $HOME: /Users/shanebecker
    $NVM_DIR: '$HOME/.nvm'
    $PREFIX: ''
    $NPM_CONFIG_PREFIX: ''
    $NVM_NODEJS_ORG_MIRROR: 'https://nodejs.org/dist'
    $NVM_IOJS_ORG_MIRROR: 'https://iojs.org/dist'
    nvm current: system
    which node: /usr/local/bin/node
    which iojs: 
    which npm: /usr/local/bin/npm
    npm config get prefix: /usr/local
    npm root -g: /usr/local/lib/node_modules
    

  • nvm ls output:


          v9.11.2
    ->       system
    default -> v9.11 (-> v9.11.2)
    node -> stable (-> v9.11.2) (default)
    stable -> 9.11 (-> v9.11.2) (default)
    iojs -> N/A (default)
    lts/* -> lts/dubnium (-> N/A)
    lts/argon -> v4.9.1 (-> N/A)
    lts/boron -> v6.17.1 (-> N/A)
    lts/carbon -> v8.16.0 (-> N/A)
    lts/dubnium -> v10.16.0 (-> N/A)
    

  • How did you install nvm? (e.g. install script in readme, Homebrew):

install script

  • What steps did you perform?

In a shell script, I installed nvm via the install script. Then I called the nvm shell script in ~/.nvm like this sh ~/.nvm/nvm.sh install 8.16.0 also from within the same shell scrip that I installed nvm from.

  • What happened?

Nothing. It quietly fails. It doesn't install the node that I specify. It also doesn't exit with a non-zero.

  • What did you expect to happen?

I expect it to install node 8.16.0

  • Is there anything in any of your profile files (.bashrc, .bash_profile, .zshrc, etc) that modifies the PATH?

  • If you are having installation issues, or getting "N/A", what does curl -I --compressed -v https://nodejs.org/dist/ print out?

```sh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

# nvm node version auto-switching - inserted by @hoverinc
if [[ -f .nvmrc && -r .nvmrc ]]; then nvm use; fi
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion
```


When I do this whole dance manually from the command line, I'm able to install nvm, then export and resource the suggested nvm bits, then install/use a node that I want. That's not my problem. What I'm trying to accomplish is to put this into a dev setup script with all of all other dev setup stuff. Help?!

Most helpful comment

sh ~/.nvm/nvm.sh install 8.16.0 this shouldn't ever work because nvm.sh must be sourced, not ran. you'd need:

. ~/.nvm/nvm.sh
nvm install 8.16.0

All 8 comments

Can you share your dev setup script? note that running this script probably isn't invoking it in a login shell, which means it wouldn't be evaluating your bashrc.

@ljharb Oops. Sorry. Forgot to paste that part.

echo "==> Installing nvm…"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
# This works as expected

echo "==> Installing Node 8.16.0 with path to script"
sh ~/.nvm/nvm.sh install 8.16.0
# This quietly fails, ie doesn't install 8.16.0 and exits 0


echo "==> Installing Node 8.16.0 with path to script"
nvm install 8.16.0
# This fails as expected: line 8: nvm: command not found

sh ~/.nvm/nvm.sh install 8.16.0 this shouldn't ever work because nvm.sh must be sourced, not ran. you'd need:

. ~/.nvm/nvm.sh
nvm install 8.16.0

Great! That was my misunderstanding. (And I feel like I remember sourcing mid-shell script would stop its execution.)

Now my last hurdle is getting the nvm use 8.16.0 to still be set after the script exits.


nvm current #=> v9.11.2

echo "==> Installing nvm…"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
# This works as expected

# This works as expected, now
. ~/.nvm/nvm.sh
nvm install 8.16.0

nvm use 8.16.0
echo `nvm current` #=> v8.16.0

nvm current #=> v9.11.2

That's not possible, since nvm operates in the current shell session - it has to be sourced and nvm used directly by the user in their own shell.

The first install will set the default; so the next time the user opens their shell, that will be used (also, install auto-uses, so install + use is redundant)

Ok. Bummer.

Is the the state of the current nvm saved in a file somewhere that I could read/write to?

There is no state - it's all in the current shell session's $PATH. nvm is per-user per-shell, so there's nothing to reify or serialize from one shell/script to another.

Dang. Oh well.

Thanks for your super quick replies. We can call this closed. 👋🏻

Was this page helpful?
0 / 5 - 0 ratings

Related issues

swateek picture swateek  ·  3Comments

goalidea picture goalidea  ·  3Comments

serbiant picture serbiant  ·  4Comments

cdelorme picture cdelorme  ·  5Comments

markedcahinde picture markedcahinde  ·  5Comments