14 August 2022

A very simple way to use NVM without sourcing it in your rc (+ works with fish)

I mainly use fish, and NVM does not work with fish out-of-the-box.

Even if I happen to be using zsh/sh/ksh/bash, I don't want to be loading NVM when I start a new shell, when I am only using it on occasion.

So I dropped the script below into my $PATH with the name with_nvm, and use it as follows: with_nvm nvm -v. Also works with stdin, which is handy.

#!/usr/bin/env sh

NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \
    . "$NVM_DIR/nvm.sh" && \
    if [ $# -gt 0 ]; then
        $@
    else
        while read -r line;
        do
            $line
        done;
    fi
Tags: NVM