nvm: nvm not available in non-interactive shells

See #1717 for installation details. Sorry if you already know these shell details - I had to review them to write this up.

Interactive shells are typed into. E.g. typing the command nvm at the flashing cursor.

This bug is about nvm not being available in non-interactive shells. E.g. this test.sh:

$ echo -e '#!/bin/bash \n nvm version' > test.sh 
$ chmod +x test.sh
$ cat test.sh                               
#!/bin/bash 
 nvm version
$ ./test.sh
./test.sh: line 2: nvm: command not found

This is because nvm isn’t on the path, which probably causes alot of other bugs.

Linking nvm.sh into the path doesn’t work because nvm.sh doesn’t appear to do anything when run directly:

./.nvm/nvm.sh version
<no output>

So, I’m a bit stuck on how to use nvm non-interactively and have to put this crufty code everywhere:

#/bin/bash
[ -s "$HOME/.nvm/nvm.sh" ] && source "$HOME/.nvm/nvm.sh" && nvm install

Which is kind of annoying. It would be nice if nvm was actually installed onto the path.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

To run nvm use via ssh remote command as part of a deployment script doing as ljharb commented makes it work.

source ~/.nvm/nvm.sh
nvm use v12.4.0
  sshCommands="$(cat <<END_HEREDOC
# Install api server dependencies.
source ~/.nvm/nvm.sh

echo "Switching to appropriate node.js version using nvm"
if ! nvm use v12.4.0 ; then
  echo "Error using nvm to switch node.js version."
  exit 14
fi

echo "Installing server api dependencies"
if ! yarn ; then
  echo "Error installing server api dependencies."
  #exit 15
fi
END_HEREDOC
)"

ssh ${deploymentUsername}@${deploymentHost} -t "$sshCommands"

@ljharb How do I use nvm in a non-interactive shell without having to source it each time?

nvm.sh is not runnable, not a binary, never will show up in a PATH, and doesn’t take a version argument. It’s a sourced shell function. Try . ~/.nvm/nvm.sh.

@ljharb I think nvm-exec is broken or not installing correctly. It doesn’t seem to do anything in bash or zsh. nvm-exec ls-remote complained ls-remote wasn’t a command. nvm-exec ls did a file listing.

michael@mcdesktop ~
☔  ./.nvm/nvm-exec ls       
bin  Desktop  Documents  Downloads  Music  NULL  Pictures  scm  snap  Templates  Videos

Aha! Great! Ok @ljharb , that’s the same direction. For whatever reason, nvm-exec doesn’t work for me in Zsh or Bash

nvm works great, but nvm-exec returns no output. Sunshine means command succeeded. Umbrella w/ rain means the command returned a failure code to the shell.

Am I using it wrong? Or did I miss some documentation?

michael@mcdesktop ~
☔  ls $NVM_DIR          
alias            Dockerfile  Makefile  package.json  test
bash_completion  install.sh  nvm-exec  README.md     update_test_mocks.sh
CONTRIBUTING.md  LICENSE.md  nvm.sh    ROADMAP.md    versions

michael@mcdesktop ~
☀  bash $NVM_DIR/nvm-exec

michael@mcdesktop ~
☀  nvm   

Node Version Manager
... (output truncated)
Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)


michael@mcdesktop ~
☀  bash $NVM_DIR/nvm-exec

michael@mcdesktop ~
☀  ./$NVM_DIR/nvm-exec 
zsh: no such file or directory: .//home/michael/.nvm/nvm-exec

michael@mcdesktop ~
☔  bash
michael@mcdesktop:~$ nvm

Node Version Manager
... (output truncated)
Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

michael@mcdesktop:~$ echo $NVM_DIR
/home/michael/.nvm
michael@mcdesktop:~$ .$NVM_DIR/nvm-exec
bash: ./home/michael/.nvm/nvm-exec: No such file or directory
michael@mcdesktop:~$ bash $NVM_DIR/nvm-exec
michael@mcdesktop:~$