Nvm: Doc: how to use nvm from non-interactive shell

Created on 13 Dec 2018  路  4Comments  路  Source: nvm-sh/nvm

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
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.

Most helpful comment

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... ;)

All 4 comments

It doesn鈥檛 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鈥檚 been around for a few years.

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

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 :-)

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... ;)

Was this page helpful?
0 / 5 - 0 ratings