Skip to content

Tip: fnm & volta setup #2048

Open
Open
@mrienstra

Description

@mrienstra

If you wish to use both volta and fnm, with fnm as the default, the following setup is working so far, though I just put it together, so who knows! (details are specific to macOS / ZSH):

  1. Make sure you don't run volta install node, so volta has no global version of node installed. Then volta will fall-through to fnm, if you're not in a directory with volta in package.json.
  2. The volta installer adds two lines to ~/.zshenv -- leave those there so that Volta will be used by non-interactive tools, e.g. for linting.
  3. Copy the volta lines from ~/.zshenv and put them in your ~/.zshrc, after fnm, to make sure volta is in your path before fnm -- in my case, that looks like the following:
eval "$(fnm env --use-on-cd)"

# Volta, copied from `.zshenv` (where it remains), to ensure it takes precedence over `fnm`
export VOLTA_HOME="$HOME/.volta"
export PATH="$VOLTA_HOME/bin:$PATH"
  1. One downside, especially if (when) you forget about this arrangement: which node will return something like ~/.volta/bin/node (rather than ~/.local/state/fnm_multishells/65437_1748363068373/bin/node), even when fnm is actually providing node. You can give "future you" a clue by adding this to your ~/.zshrc:
if [[ $- == *i* && -t 1 ]]; then
  autoload -U colors && colors
  which() {
    if [[ "$1" == "node" || "$1" == "npm" || "$1" == "yarn" || "$1" == "pnpm" ]]; then
      if [[ -z "${fg[green]}" || -n "$NO_COLOR" ]]; then
        echo "💡To locate the actual binary: 'volta which $1'" >&2
      else
        echo "💡To locate the actual binary: ${bold_color}${fg[green]}volta which $1${reset_color}" >&2
      fi
    fi
    command which "$@"
  }
fi

(I posted the same comment in the fnm repo, figured it might be useful to others)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions