Maintain local Node.js installation with nvm

I mostly use containers for development, but sometimes a local Node.js development environment is needed. I like to use nvm to manage my environment.

nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.

In short worknotes format, a rundown how to install, and especially update my environment is as follows.

Prerequisites

These notes might work on many platforms, but I use macOS Big Sur at the time of writing.

Install and configure a Node.js version

Install nvm itself:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Check the latest official guide from the nvm website linked above.

List available Node.js versions:

nvm ls-remote

Install desired version:

nvm install v14.16.0

When you have multiple versions installed, and in any case it's good to set a default Node.js version:

nvm alias default v14.16.0

Install the packages which are needed globally:

npm install -g [package-name]

And that's it!

Update nvm

Updating nvm itself is same as installing:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Update Node.js version and reconfigure

Updating Node.js version involves a couple of additional steps compared to the initial installation:

List installed node versions, and take note of the default version:

nvm ls

Activate the one, you want to update. Then, list globally installed packages and take note of them:

npm list -g --depth=0

List available Node.js versions for the current major version line:

nvm ls-remote v14

Since a new version is available, install new version and reinstall packages from old version:

nvm install v14.16.1 --reinstall-packages-from=v14.16.0

If I want to install a clean version, without global packages, instead of the previous just run:

nvm install v14.16.1

Set default node version to point to the latest:

nvm alias default v14.16.1

Check locally installed versions, and the alias mappings again:

nvm ls

Test that the new version works. After, remove the obsolete old version:

nvm uninstall v14.16.0

All done, start coding!