Managing Node.js versions with anyenv & nodenv on macOS (for fish shell)

KJ
TechNest
Published in
2 min readJan 30, 2020
Photo by David Clode on Unsplash

Use nodenv to manage multiple Node.js versions. As stated in the official Wiki, one of the biggest reasons to choose nodenv over the others is that “nodenv can automatically select the appropriate node version for a given project”. If you are currently using nodebrew and feel inconvenienced, consider using nodenv.

anyenv

First, install anyenv to manage nodenv. anyenv is installed using Homebrew. If you don’t have it yet, install it beforehand.

1.Install anyenv:

> brew install anyenv

2.Set up anyenv in your shell:

> anyenv init
# Load anyenv automatically by adding
# the following to ~/.config/fish/config.fish:
status --is-interactive; and source (anyenv init -|psub)

3.Add the above code to the fish config file:

> echo 'status --is-interactive; and source (anyenv init -|psub)' >> ~/.config/fish/config.fish

If you do not have the config.fish file on your computer, create one:

> cd ~/.config/fish/
> touch config.fish

Then try again.

4.Reload your shell (for your changes to take effect):

> exec $SHELL -l

5.Create manifest directory:

> anyenv install --init

The settings of anyenv are completed. Next, install nodenv.

nodenv

1.Install nodenv:

> anyenv install nodenv

2.Reload your shell

> exec $SHELL -l

3.List all available versions:

> nodenv install -l

4.Install a Node version:

> nodenv install 12.14.1

5.Set the global version of Node:

> nodenv global 12.14.1

6.Reload your shell

> exec $SHELL -l

7.Check the Node version:

> nodenv version
12.14.1 (set by /Users/kjmczk/.anyenv/envs/nodenv/version)

Let’s check it with the node command:

> node -v
v12.14.1

It worked!

The basic settings of nodenv are completed. To set a local version specific to the current directory, go to the next section.

8.Set a local application-specific Node version:

> cd myproject
> nodenv install 13.7.0
> nodenv local 13.7.0

9.Check the Node version in the current directory:

> nodenv version
13.7.0 (set by /Users/kjmczk/dev/myproject/.node-version)

and

> node -v
v13.7.0

10.List all Node versions known to nodenv:

> nodenv versions
system
12.14.1
* 13.7.0 (set by /Users/kjmczk/dev/myproject/.node-version)

An asterisk appears next to the currently active version.

That’s it 🎉

Not only nodenv but pyenv and rbenv can be set similarly.

Update all **env (Added on Sept 28, 2020)

Add an anyenv plugin anyenv-update:

mkdir -p (anyenv root)/plugins
git clone https://github.com/znz/anyenv-update.git (anyenv root)/plugins/anyenv-update

Run the command to update all **env:

anyenv update

The list of available (Node, Python, …) versions are now updated.

--

--