Skip to content
On this page

Purpose:

the "NVM Installer" wraps the core actions NVM provides into a version-pinning deploy step that in one call, keeps dev environments and prod environments in sync (including refreshing node modules folder when there's a change to package.json.)

How to:

The script is served from this site as /js/nvmi.

You can download it, or run it inline:

bash -c "$(curl -fsSL https://ashnazg.com/js/nvmi)"

You can pin to specific versions of NVM, node, and/or yarn with envvars:

bash -c "nvm_ver=0.33.2 node_ver=12.19.0 yarn_ver=1.22.10 $(curl -fsSL https://ashnazg.com/js/nvmi)"

for node_ver, it can be a pinned version like 12.19.0, or the special value of lts

Expected Results

If invoked without a script-target, it'll...

  1. install NVM if not found (in user-space, no sudo)
    • if installing NVM, wire your shell's rc to activate nvm (unless you use --no-rc.)
      • it writes to .bash_profile or .zprofile for bash or zsh, but you can use --rc=~/your_rc to point it to any sh-like init file.
  2. ensure NVM has deployed the targetted node-version, and activate it. (but only for this run. If you add --set-default it'll make this node-version your general default.)
  3. ensure yarn is present (and its and npm's version matches pins)
  4. if any of the above required action, or your project's package.json has changed since nvmi last ran, it'll run yarn. (you can skip this to make nvmi CWD-agnostic with --no-project.)
  5. if a dependency is locally available through a yarn link, attach to that instead of going to network. (unless disabled with --no-auto-link)
2023-05-06T15:04:06-07:00 NVMI@0.9.9 branchname@c2ee3a6e50e210b422ba34038083a2373b9d779d is checking on node tools in ~/servicefolder
nvm expected: 0.38.0 actual: 0.38.0
node expected: 20.0.0 actual: v20.0.0
npm expected: 9.1.1 actual: 9.1.1
yarn expected: 1.22.19 actual: 1.22.19

Running Specific Commands Inside NVM's Context

NVMI's originally a wrapper for scripts as defined in package.json:

package.json {
	"scripts": {
		"test": "echo \"Error: no test specified\" && exit 1"
	}
}

So running nvmi test against the above will result in:

2023-05-06T15:04:06-07:00 NVMI@0.9.9 branchname@c2ee3a6e50e210b422ba34038083a2373b9d779d is checking on node tools in ~/servicefolder
nvm expected: 0.38.0 actual: 0.38.0
node expected: 20.0.0 actual: v20.0.0
npm expected: 9.1.1 actual: 9.1.1
yarn expected: 1.22.19 actual: 1.22.19
yarn run v1.22.19
$ echo "Error: no test specified" && exit 1
Error: no test specified
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

But it also supports arbitrary shell commands: nvmi --shell ls -- --version (the -- is to turn off nvmi's options-parsing)

Both kinds of task-runs can be added to bash+curl mode by tacking them on the end:

bash -c "node_ver=18.16.0 $(curl -fsSL https://ashnazg.com/js/nvmi)" -- --shell ls
  • I'm using -- to tell bash the rest goes straight to NVMI; it doesn't effect all CLI constructions, but doing so prevents gotchas. This recurses: to get ls --version without nvmi responding with its own versions, run
    bash -c "node_ver=18.16.0 $(curl -fsSL https://ashnazg.com/js/nvmi)" -- --shell ls -- --version

JavaScript/Bash source released under the MIT License.