Skip to content

Instantly share code, notes, and snippets.

@rtfpessoa
Created August 26, 2016 11:42
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save rtfpessoa/811701ed8fa642f60e411aef04b2b64a to your computer and use it in GitHub Desktop.
Save rtfpessoa/811701ed8fa642f60e411aef04b2b64a to your computer and use it in GitHub Desktop.
NVM lazy loading script
#!/bin/bash
#
# 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:
# * https://www.reddit.com/r/node/comments/4tg5jg/lazy_load_nvm_for_faster_shell_start/d5ib9fs
# * http://broken-by.me/lazy-load-nvm/
# * https://github.com/creationix/nvm/issues/781#issuecomment-236350067
#
NVM_DIR="$HOME/.nvm"
# 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`)
fi
NODE_GLOBALS+=("nvm")
load_nvm () {
# Unset placeholder functions
for cmd in "${NODE_GLOBALS[@]}"; do unset -f ${cmd} &>/dev/null; done
# Load NVM
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
# (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} \$@; }"
fi
done
@Fastjur
Copy link

Fastjur commented May 22, 2020

EDIT: People finding this, yes that works just fine :)

So I just source this from ~/.zshrc right? That appears to work but just making sure!

@jediahkatz
Copy link

Is it possible to integrate this with https://github.com/so-fancy/diff-so-fancy as the git pager? When I do

git config --global core.pager "diff-so-fancy | less --tabs=4 -RFX"

Then attempting git diff before nvm is loaded yields:

diff-so-fancy | less --tabs=4 -RFX: 1: diff-so-fancy: not foun

@Fastjur
Copy link

Fastjur commented Feb 12, 2021

Yeah it doesn't always work for me, I just first run nvm so everything is loaded and then run the commands needed.

@rtfpessoa
Copy link
Author

Just run https://github.com/asdf-vm/asdf-nodejs or https://github.com/nodenv/nodenv directly.
NVM was never great and specially now there are much better options.

@Fastjur
Copy link

Fastjur commented Feb 17, 2021

Yeah I've been told that me using nvm is already considered outdated. I'll check one of these out soon, thanks.

@gearonix
Copy link

Thanks. worked

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