Bash – nvm command not available in bash script

bashnode.jsshell-scriptsoftware installation

I am trying to build a script in in which nvm and eventually node will get installed.
I have installed nvm with cURL. I see the modifications in the .profile or .bashrc file (both work) and when typing the nvm at the bash prompt, it shows the options available etc.
So nvm works. Manually I can install node, but as soon as I put the nvm command in a shell script:

nano test.sh

#!/bin/bash
nvm

and run it with:

chmod 755 test.sh
./test.sh

I get:

./test.sh: line 2: nvm: command not found

If it can't find nvm, I don't even have to think of

nvm ls-remote 

or

nvm install ...

I got Ubuntu 14.04 installed and Bash is my shell.

Best Answer

nvm command is a shell function declared in ~/.nvm/nvm.sh.

You may source either of following scripts at the start of yours to make nvm() available:

. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc
. $(brew --prefix nvm)/nvm.sh  # if installed via Brew
Related Question