NVM setup is not quite correct

nodejs

#1

It is very convenient that cloud 9 includes NVM “in the box”. Unfortunately, the NVM set up is not quite correct. The script lines to configure it are provided by default in the ~/.profile file, so they are only available in a “login shell”. They don’t work, without extra work, if you try to use them in (for example) a script. This can be worked around, but is inconvenient.

Hopefully I’m not the only one to notice this, and others will join me in requesting that cloud 9 workspaces as created, set up NVM more correctly, and one of the other ~/.bash_* files, so that they are available in all bash contexts.


#2

Interesting it looks like behavior of nvm install script have changed since we have added it to cloud9 default docker image https://github.com/creationix/nvm/pull/511/files
But cloud9 always uses login shells (partly because of nvm not being detected back then) so it should work.
Could you tell how do you run the script for it to not get correct nvm?


#3

It is easy to see the problematic behavior. Make a bash script. Try to use NVM inside. It does not work, it fails because the command is not found. It is possible to get around this by “sourcing” the script, and it will not work if you just run the script.

In other words, the fact that c9 itself always makes a login shell when you make a top-level terminal window, does not solve this problem, it needs to be solved by making the commands available from any bash context not only from a login shell.


#4

Interesting nvm is indeed not callable from a script without sourcing, but seems that is not related to bashrc/profile, since nvm is declared as a bash function, and i get the same error in both cases when running the following script:

#!/bin/bash
echo $NVM_DIR
nvm use 4

#5

To make it available to a bash sub-shell, i added this to ~/.bashrc:

export NVM_DIR="/home/ubuntu/.nvm"
[ “BASH_VERSION" ] && npm() { if [ "*” == “config get prefix” ]; then which node | sed “s/bin/node//”; else (which npm) "@"; fi } # hack: avoid slow npm sanity check in nvm
[ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh” # This loads nvm
unset npm # end hack

… but that still does not make it available inside a script as you described. For that, I found the easiest way is to just put those same commands, or even the simpler “. ~/.nvm/nvm.sh” in the script itself. From some research online, it sounds like most people just do this when they need access to NVM inside a bash script. Oh well.