Effortless Node.js Version Management with the Fish Shell


Motivation #

Continuing our exploration of the Fish shell and its synergy with prevalent tools, this post follows the prior piece focused on Python’s virtualenv.

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

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

Node.js 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, renowned for managing multiple Node.js versions on a single machine, facilitates easy installation and swapping between different Node.js versions with a single command.

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

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

The Method #

Objective #

The goal is to have Node.js 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.

The How #

The implementation 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, which is a utility for executing Bash commands from Fish shell. It’s a simple plugin that can be installed through fisher or manually.

fisher install edc/bass

I suggest using fisher as a simplistic plugin manager for Fish. I’ve authored a plugin that can be easily installed through 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, execute the following command:

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 versions. If the required version of Node.js 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. 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.