Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nvm not available in non-interactive shells #1718

Closed
MichaelJCole opened this issue Feb 1, 2018 · 18 comments
Closed

nvm not available in non-interactive shells #1718

MichaelJCole opened this issue Feb 1, 2018 · 18 comments

Comments

@MichaelJCole
Copy link

MichaelJCole commented Feb 1, 2018

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.

@ljharb
Copy link
Member

ljharb commented Feb 2, 2018

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.

@MichaelJCole
Copy link
Author

@ljharb Hey Jordan, no problem. I get that. But it doesn't show up in non-interactive shells, so it can't be scripted. I noticed a nvm-exec file but didn't understand it's purpose.

What about a nvm file that sources nvm.sh and runs it with arguments?

The installer could link this into ~/bin or if no directory explain what to do.

@basickarl
Copy link

basickarl commented Feb 6, 2018

I'd also like it to show up in non-interactive shells. In my example, I have a Docker image with NVM installed on a certain user. I'd like to start a new container and have NVM sourced when starting.

@ljharb
Copy link
Member

ljharb commented Feb 7, 2018

@MichaelJCole that's exactly what nvm-exec is for.

@MichaelJCole
Copy link
Author

MichaelJCole commented Feb 7, 2018

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:~$ 

@ljharb
Copy link
Member

ljharb commented Feb 9, 2018

That's because you're running bash $NVM_DIR/nvm-exec - nvm-exec is an executable. Try nvm-exec $version $command.

@MichaelJCole
Copy link
Author

MichaelJCole commented Feb 11, 2018

@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

@ljharb
Copy link
Member

ljharb commented Feb 12, 2018

@MichaelJCole that’s how it’s supposed to work. It takes an arbitrary command and executes it with a particular version of node nvm used.

@MichaelJCole
Copy link
Author

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

@ljharb
Copy link
Member

ljharb commented Feb 18, 2018

I mean, really, you don't - one way or another, nvm.sh must be sourced to use nvm.

However, if you execute nvm-exec with the proper arguments, it will source it each time for you.

@MichaelJCole
Copy link
Author

MichaelJCole commented Feb 22, 2018

@ljharb ok, this isn't fitting my model, but that's ok.

For most projects, I've got a preview.sh or deploy.sh to start them up. Something like this for a hexo project:

#!/bin/bash

# Load nvm (node version manager), install node (version in .nvmrc), and npm install packages
[ -s "$HOME/.nvm/nvm.sh" ] && source "$HOME/.nvm/nvm.sh" && nvm use
# Use local-npm if running
wget -qO /dev/null 'http://127.0.0.1:5080/_browse/' && npm set registry http://127.0.0.1:5080 && echo '----> local-npm found <----'
# Npm install if not already.
[ ! -d "node_modules" ] && npm install

hexo server

Looking at this again, I'm whining about the first parts of the first line. So maybe not a big deal. Mostly, it's not very new developer friendly.

Do you have any suggestions or do you see a better way? Thanks!

@ljharb
Copy link
Member

ljharb commented Feb 23, 2018

I don't think there's a better way, no (altho the && nvm use is redundant; nvm auto-runs that for you).

@MichaelJCole
Copy link
Author

Ok, cool. Thanks!

@JimLynchCodes
Copy link

I would like to use "nvm use" in my cron scripts and am unable to run it. Neither nvm-exec nor nvm.sh are working...

~/.nvm/nvm.sh use v11
^ does nothing and does not switch to version 11...

~/.nvm/nvm-exec use v11
^ outputs the error: "/home/ubuntu/.nvm/nvm-exec: line 15: exec: use: not found"

@ljharb what's the correct way to do this?

@ljharb
Copy link
Member

ljharb commented Apr 24, 2020

@JimLynchCodes your cron scripts need to source nvm.sh to be able to use nvm.

@LiamKarlMitchell
Copy link

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"

mzagaja added a commit to MAPC/datacommon that referenced this issue Oct 7, 2020
* Reading nvm-sh/nvm#1718 suggests that we need source the nvm.sh file in order for nvm to work/apply before executing other commands in a non-interactive shell.
@promisepreston
Copy link

In windows I had to do this

C:\Node\nvm\nvm use 14.15.4

Where C:\Node\nvm is the directory where I stored nvm

Then I restarted the Jenkins server

@ljharb
Copy link
Member

ljharb commented Jan 14, 2021

@promisepreston nvm does not support windows, unless you're using WSL. Perhaps you're using nvm-windows, a different project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants