Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created June 21, 2019 00:35
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fideloper/903f8976206fd6ca847c05f792ac9ea8 to your computer and use it in GitHub Desktop.
Save fideloper/903f8976206fd6ca847c05f792ac9ea8 to your computer and use it in GitHub Desktop.
Defer loading of NVM to increase new terminal init speed
# Thanks to https://www.growingwiththeweb.com/2018/01/slow-nvm-init.html, modified
# for zsh as "type -t" works in bash but not zsh
# Add this to your .zshrc ...
# 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" ] && [ ! "$(type -w __init_nvm | awk '{print $2}')" = function ]; then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
declare -a __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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment