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

Doc: how to use nvm from non-interactive shell #1959

Open
dtgriscom opened this issue Dec 13, 2018 · 4 comments
Open

Doc: how to use nvm from non-interactive shell #1959

dtgriscom opened this issue Dec 13, 2018 · 4 comments

Comments

@dtgriscom
Copy link

dtgriscom commented Dec 13, 2018

As nvm is implemented as a shell function, it won't work in non-interactive shells where the shell initialization (e.g. .bashrc) isn't performed. To make this easier, how about adding the following tool to the documentation (with a suggested name of nvm-run):

#!/usr/bin/env bash

NVM_SH="${HOME}/.nvm/nvm.sh"

if [ ! -r "$NVM_SH" ]; then
	echo "Can't find nvm at \"$NVM_SH\""
	exit 1
fi

\. "$NVM_SH" --no-use

nvm $@

This could then be used to execute any desired nvm command, e.g. (in Ubuntu 18, with nvm initialization in .bashrc disabled):

linuxdev18:webgui griscom$ nvm

Command 'nvm' not found, did you mean:

  command 'nvim' from deb neovim
  command 'kvm' from deb qemu-kvm
  command 'pvm' from deb pvm
  command 'nvme' from deb nvme-cli
  command 'nim' from deb nim
  command 'nm' from deb binutils
  command 'vm' from deb mgetty-voice
  command 'nvi' from deb nvi
  command 'num' from deb quickcal
  command 'nam' from deb nam
  command 'npm' from deb npm
  command 'lvm' from deb lvm2

Try: apt install <deb name>
linuxdev18:webgui griscom$ ./nvm-run exec --silent 8.12.0 npm --version
6.4.1
linuxdev18:webgui griscom$ ./nvm-run exec --silent 8.11.4 npm --version
5.6.0
linuxdev18:webgui griscom$ 

(Note: for some reason this doesn't seem to work with ./nvm-run current (it always reports "system"), but since most people will use this to run a command with a specific version of node this may not be a problem.)

If this works for the nvm developers, I'd be happy to submit a pull request; alternately, I'd be fine with someone else adding this to the doc.

@ljharb
Copy link
Member

ljharb commented Dec 13, 2018

It doesn’t require an interactive shell, but it does require a login shell.

You may be interested in the existing `nvm-exec command, that does what you want - it’s been around for a few years.

@dtgriscom
Copy link
Author

I've looked at nvm-exec, and it does not seem to do what I want. Documentation was non-existent (not even a mention in the ReadMe), so I was a bit feeling in the dark, but as far as I can tell it assumes that you want to run nvm use $VERSION; <command>, which for me isn't always the case, e.g. I'm using this nvm-run tool to install missing Node.js versions from a Makefile.

So, reasons I prefer this nvm-run over the existing nvm-exec:

  • Can run nvm commands other than use
  • Will (hopefully) be better-documented
  • Doesn't require the user to set a NODE_VERSION variable; can just use the arguments like always

@ljharb
Copy link
Member

ljharb commented Dec 13, 2018

ah, that makes sense. nvm run is already an nvm command - so maybe you want more like nvm shell or something.

As for poor docs on nvm-exec, that is indeed a problem, but that's fixable with a PR :-)

@dtgriscom
Copy link
Author

Well, the name shouldn't be confused with an existing command, so nvm-run probably isn't the best. But, if it's just to be a documentation suggestion for the user rather than an included file, then it doesn't really matter, and nvm-shell works for me.

On documenting nvm-exec, I first have to understand what it does... ;)

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

2 participants