Skip to content

Instantly share code, notes, and snippets.

@eblocha
Last active August 30, 2023 02:01
Show Gist options
  • Save eblocha/20dd80d911df7d9af64300160aabcb6b to your computer and use it in GitHub Desktop.
Save eblocha/20dd80d911df7d9af64300160aabcb6b to your computer and use it in GitHub Desktop.
Custom fnm env
if type fnm > /dev/null; then
export FNM_MULTISHELL_ROOT="/run/user/$UID/fnm_multishells"
export FNM_LOGLEVEL="info"
export FNM_VERSION_FILE_STRATEGY="local"
export FNM_MULTISHELL_PATH="$FNM_MULTISHELL_ROOT/$$"
export FNM_COREPACK_ENABLED="false"
export FNM_DIR="$HOME/.local/share/fnm"
export FNM_ARCH="x64"
export FNM_RESOLVE_ENGINES="false"
export FNM_NODE_DIST_MIRROR="https://nodejs.org/dist"
export PATH="$FNM_MULTISHELL_PATH/bin":$PATH
autoload -U add-zsh-hook
_fnm_on_exit () {{
rm -f "$FNM_MULTISHELL_PATH"
}}
add-zsh-hook zshexit _fnm_on_exit && _fnm_on_exit
mkdir -p "$FNM_MULTISHELL_ROOT"
ln -s "$FNM_DIR/aliases/default" "$FNM_MULTISHELL_PATH"
rehash
_fnm_autoload_hook () {{
if [[ -f .node-version || -f .nvmrc ]]; then
fnm use --silent-if-unchanged
fi
}}
add-zsh-hook chpwd _fnm_autoload_hook
fi
@eblocha
Copy link
Author

eblocha commented Aug 18, 2023

This is my customized setup in .zshrc for fnm, which is a much faster alternative to nvm for managing multiple node installs.

Key changes to output from fnm env:

  1. I'm using the current pid of the shell for the symlink path instead of the pid of the fnm process when generating the env commands. This is easier to reason about, and it allows me to query ps -p <pid> to detect dangling links.
  2. I added a zshexit hook to remove the symlink when the shell exits. Taken from the experimental branch in fnm here.
  3. Added back in the --use-on-cd option effect.

I skip all of this if fnm is not installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment