Skip to content

Instantly share code, notes, and snippets.

Last active April 13, 2023 20:35
Star You must be signed in to star a gist
What would you like to do?
# lazyload nvm
# all props goes to
# grabbed from reddit @
lazynvm() {
unset -f nvm node npm npx
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/" ] && . "$NVM_DIR/" # This loads nvm
if [ -f "$NVM_DIR/bash_completion" ]; then
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
nvm() {
nvm $@
node() {
node $@
npm() {
npm $@
npx() {
npx $@
Copy link

king-11 commented Sep 16, 2020

@laur89 thanks for your snippet and reply. So I just used a combined script took some parts from yours and some from @christophemarois now this works with yarn globals and node version is same for projects using yarn as well.

export NVM_DIR="$HOME/.nvm"
#[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"  # This loads nvm
#[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
NODE_GLOBALS+=(node nvm yarn)

_load_nvm() {
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

for cmd in "${NODE_GLOBALS[@]}"; do
eval "function ${cmd}(){ unset -f ${NODE_GLOBALS[*]}; _load_nvm; unset -f _load_nvm; ${cmd} \$@; }"
unset cmd NODE_GLOBALS

export PATH="$PATH:$HOME/.yarn/bin"```

Copy link

creio commented Nov 13, 2020


export NVM_DIR="$HOME/.config/nvm"

# Lazy load
if [[ -s "$NVM_DIR/" ]]; then
  NODE_GLOBALS=(`find $NVM_DIR/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
  # Lazy-loading nvm + npm on node globals
  load_nvm () {
    [ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"
  # Making node global trigger the lazy loading
  for cmd in "${NODE_GLOBALS[@]}"; do
    eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$@ }"

Copy link

throrin19 commented Feb 26, 2021

It works fine but vscode tasks does not works with lazy loading 👎

Copy link

laur89 commented Jul 30, 2021

Any idea if this solution still has merit after nvm-sh/nvm#2317?
Edit, in my case with bash, it still beats the original/vanilla by ~0.4s

Copy link

This does not work incase a script is run directly ./script.js.
script.js looks like:

#!/usr/bin/env node
# everything else

because env tries to find node within current paths which is not updated by the nvm due to lazy load.

solution that I found works is to create individual shell script for each of the command in the path which would trigger lazynvm(). nvm will then initialize node path at the beginning of the $PATH so that the next node call will run the node from the right path.

[ -s "$NVM_DIR/" ] && . "$NVM_DIR/"
. $(dirname "$0")/lazynvm
node $@

and then add both to $PATH in your .zshrc:

export PATH="~/lazy-load:$PATH"  # assuming both files are inside ~/lazy-load dir

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