How to set up nvm to work with fish shell?

ryunosukezoran

Zoran Stankovic

Posted on November 23, 2021

How to set up nvm to work with fish shell?

Recently, I switched from oh-my-zsh to a fish shell, and I didn’t regret it. Fish is a nice and user-friendly command-line shell. fish supports powerful features like syntax highlighting, autosuggestions, and tab completions that work, with nothing to learn or configure.
But when I tried to install nvm (Node Version Manager) I had a problem, the shell didn’t recognize the nvm command. After some investigation, I found the solution.
First, you need to install a fisher. Fisher is a package manager for the fish shell. Download fisher.fish to your functions directory or any directory on your function path.

curl https://git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish
Enter fullscreen mode Exit fullscreen mode

Your shell can take a few seconds before loading newly added functions. If the fisher command is not immediately available, launch a new session or replace the running shell with a new one.
After installing the fisher, you need to add the Bass package. Bass makes it easy-to-use utilities written for Bash in a fish shell. Using fisher:

fisher add edc/bass
Enter fullscreen mode Exit fullscreen mode

Create a new fish file for nvm:

touch ~/.config/fish/functions/nvm.fish
Enter fullscreen mode Exit fullscreen mode

And add fish function to it to load nvm:

function nvm
    bass source ~/.nvm/nvm.sh -- no-use ‘;’ nvm $argv
end
Enter fullscreen mode Exit fullscreen mode

And you are ready-to-use NVM.

How does NVM work?

If you want to download, compile, and install the latest release of node you just need to do this:

nvm install node # “node” is an alias for the latest version
Enter fullscreen mode Exit fullscreen mode

To install a specific version of node:

nvm install 10.10.0 # or 8.9.1, etc.
Enter fullscreen mode Exit fullscreen mode

Important to know is that the first version installed becomes the default. New shells start with the default version of node.
To set a default Node version in any new shell, use the alias ‘default’:

nvm alias default [version of node] # e.g.
nvm alias default 12.13.1
Enter fullscreen mode Exit fullscreen mode

If you want to use a specific version of NodeJS for your project, you can specify the NodeJS version inside of .nvmrc file inside of your project e.g. 12.13.1 and then activate it using nvm use command in your shell.

💖 💪 🙅 🚩
ryunosukezoran
Zoran Stankovic

Posted on November 23, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related