Skip to content

Instantly share code, notes, and snippets.

@dhvcc
Created October 13, 2021 11:34
Show Gist options
  • Save dhvcc/36368e04babe40afe8bfc82cda64875a to your computer and use it in GitHub Desktop.
Save dhvcc/36368e04babe40afe8bfc82cda64875a to your computer and use it in GitHub Desktop.
Defer nvm initialization to improve shell load time [ZSH]
# Defer initialization of nvm until nvm, node or a node-dependent command is
# run. Ensure this block is only run once if .bashrc gets sourced multiple times
# by checking whether __init_nvm is a function.
if [ -s "$HOME/.nvm/nvm.sh" ] && [ ! "$(whence -w __init_nvm)" = function ]; then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
__node_commands=('nvm' 'node' 'npm' 'yarn' 'gulp' 'grunt' 'webpack')
function __init_nvm() {
for i in "${__node_commands[@]}"; do unalias $i; done
. "$NVM_DIR"/nvm.sh
unset __node_commands
unset -f __init_nvm
}
for i in "${__node_commands[@]}"; do alias $i='__init_nvm && '$i; done
fi
@dhvcc
Copy link
Author

dhvcc commented Oct 13, 2021

It's basically a ZSH version of this comment

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