Effortless Node.js Version Management with the Fish Shell
Tags: node, nvm, fish, programming
Published: 2023-06-10 22:46:44

## Preamble

Continuing our exploration of the [Fish shell][fish] and its synergy with prevalent tools, this post follows the [prior piece][fish-virtualenv] focused on [Python][python]'s [virtualenv][virtualenv].

This write-up provides a straightforward strategy to incorporate automatic [Node.js][nodejs] version control with the [Fish shell][fish], whilst avoiding resource-hungry frameworks like [oh-my-fish][omf] which can often slow down the shell.

## A Brief on Node.js Version Management (nvm)

[Node.js][nodejs] is a widely embraced JavaScript runtime environment, enabling the execution of JavaScript code outside a browser. It finds utility in the creation of server-side apps, command-line tools, and even desktop and mobile apps.

[nvm][nvm], renowned for managing multiple [Node.js][nodejs] versions on a single machine, facilitates easy installation and swapping between different Node.js versions with a single command.

[nvm][nvm], while built for [Bash][bash] and [Zsh][zsh] shells, isn't directly compatible with the [Fish shell][fish]. There are several ways to mesh [nvm][nvm] with the [Fish shell][fish], but they are often too intricate or resource-consuming.

> In my experience, attempting to use an NVM clone in [Fish shell][fish] fell short of my expectations. It failed to accomplish the primary task of automatically shifting between [Node.js][nodejs] versions when I switch the directory based on the `.nvmrc` file.

## The Method

### Objective

The goal is to have [Node.js][nodejs] versions switch automatically when the directory is changed, based on the `.nvmrc` file.

Why not use `.node-version`? The `.nvmrc` file is favored due to its support for additional options like `iojs` and `lts/*`, plus being a standard for [nvm][nvm].

### The How

The [implementation][nvmrc] is uncomplicated as usual. It doesn't entail multiple dependencies or complex logic. It's essentially a few lines of code that get the job done.

### Setting It Up

The plugin is dependent on another plugin [edc/bass](https://github.com/edc/bass), which is a utility for executing Bash commands from [Fish shell][fish]. It's a simple plugin that can be installed through [fisher][fisher] or manually.

```fish
fisher install edc/bass
```

I suggest using [fisher][fisher] as a simplistic plugin manager for Fish. I've authored a plugin that can be easily installed through [fisher][fisher] and amalgamated into any Fish environment. Should you have reasons not to use Fisher, simply copy-paste `nvm.fish` files from all directories to your `~/.config/fish/` in their respective ones.

To set up the plugin with [fisher][fisher], execute the following command:

```fish
fisher install aohorodnyk/fish-nvm
```

### How to Use

Upon changing the directory based on the `.nvmrc` file, the plugin will automatically switch between [Node.js][nodejs] versions. If the required version of [Node.js][nodejs] isn't installed, it will be installed automatically.

#### Configurations

No particular configurations are required for this plugin. However, you can configure it by setting the below environment variables:

* `nvmrc_enable` (default: `yes`): Toggles Fish NVM on or off. Set to `no` to disable.
* `nvmrc_announce` (default: `yes`): Toggles on or off the announcement of the Node.js version change. Set to `no` to disable.

## Epilogue

Don't hesitate to utilize the [Fish shell][fish]. It's a fast, versatile, and user-friendly shell that stands as a strong contender to bash and zsh.

Contributing to open-source is a great way to learn, assist others, and help make the technological world simpler and better for all.

[fish]: https://fishshell.com/
[virtualenv]: https://virtualenv.pypa.io/
[fish-virtualenv]: https://aohorodnyk.com/post/2023-06-10-fish-autovenv/
[nvmrc]: https://github.com/aohorodnyk/fish-nvm
[python]: https://www.python.org/
[fisher]: https://github.com/jorgebucaran/fisher
[nodejs]: https://nodejs.org/
[nvm]: https://github.com/nvm-sh/nvm
[bash]: https://www.gnu.org/software/bash/
[zsh]: https://www.zsh.org/
[omf]: https://github.com/oh-my-fish/oh-my-fish