Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NVM lazy loading script
# NVM lazy loading script
# NVM takes on average half of a second to load, which is more than whole prezto takes to load.
# This can be noticed when you open a new shell.
# To avoid this, we are creating placeholder function
# for nvm, node, and all the node packages previously installed in the system
# to only load nvm when it is needed.
# This code is based on the scripts:
# *
# *
# *
# Skip adding binaries if there is no node version installed yet
if [ -d $NVM_DIR/versions/node ]; then
NODE_GLOBALS=(`find $NVM_DIR/versions/node -maxdepth 3 \( -type l -o -type f \) -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
load_nvm () {
# Unset placeholder functions
for cmd in "${NODE_GLOBALS[@]}"; do unset -f ${cmd} &>/dev/null; done
# Load NVM
[ -s "$NVM_DIR/" ] && . "$NVM_DIR/"
# (Optional) Set the version of node to use from ~/.nvmrc if available
nvm use 2> /dev/null 1>&2 || true
# Do not reload nvm again
export NVM_LOADED=1
for cmd in "${NODE_GLOBALS[@]}"; do
# Skip defining the function if the binary is already in the PATH
if ! which ${cmd} &>/dev/null; then
eval "${cmd}() { unset -f ${cmd} &>/dev/null; [ -z \${NVM_LOADED+x} ] && load_nvm; ${cmd} \$@; }"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.