# lazyload nvm | |
# all props goes to http://broken-by.me/lazy-load-nvm/ | |
# grabbed from reddit @ https://www.reddit.com/r/node/comments/4tg5jg/lazy_load_nvm_for_faster_shell_start/ | |
lazynvm() { | |
unset -f nvm node npm npx | |
export NVM_DIR=~/.nvm | |
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm | |
if [ -f "$NVM_DIR/bash_completion" ]; then | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion | |
fi | |
} | |
nvm() { | |
lazynvm | |
nvm $@ | |
} | |
node() { | |
lazynvm | |
node $@ | |
} | |
npm() { | |
lazynvm | |
npm $@ | |
} | |
npx() { | |
lazynvm | |
npx $@ | |
} |
This comment has been minimized.
This comment has been minimized.
updated for CLI npx and bash completion |
This comment has been minimized.
This comment has been minimized.
Thanks @christophemarois, I've been hunting for a solution like this one! |
This comment has been minimized.
This comment has been minimized.
@christophemarois this is gold, thanks |
This comment has been minimized.
This comment has been minimized.
Updated @christophemarois's script with
Thanks for sharing! |
This comment has been minimized.
This comment has been minimized.
I got this error with @christophemarois and @VincentN script:
I changed eval "${cmd}(){ unset -f ${NODE_GLOBALS} >/dev/null 2>&1; load_nvm; ${cmd} \$@ }" to eval "${cmd}(){ unset -f ${cmd} >/dev/null 2>&1; load_nvm; ${cmd} \$@; }" Note the change of |
This comment has been minimized.
This comment has been minimized.
@adriaanvanrossum, oops. Updated the script! |
This comment has been minimized.
This comment has been minimized.
Unsetting
|
This comment has been minimized.
This comment has been minimized.
@VincentN is this the approach I should take if I want it to be applied to |
This comment has been minimized.
This comment has been minimized.
@laur89 I ran into this error while using your code snippet also I am not able to use yarn globals using any of the above snippets @VincentN @christophemarois please any suggestion |
This comment has been minimized.
This comment has been minimized.
This suggests you're not using bash, as |
This comment has been minimized.
This comment has been minimized.
@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.sh" ] && \. "$NVM_DIR/nvm.sh" # 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.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -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} \$@; }"
done
unset cmd NODE_GLOBALS
export PATH="$PATH:$HOME/.yarn/bin"``` |
This comment has been minimized.
This comment has been minimized.
export NVM_DIR="$HOME/.config/nvm"
# Lazy load
if [[ -s "$NVM_DIR/nvm.sh" ]]; then
NODE_GLOBALS=(`find $NVM_DIR/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
NODE_GLOBALS+=("node")
NODE_GLOBALS+=("nvm")
# Lazy-loading nvm + npm on node globals
load_nvm () {
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
}
# Making node global trigger the lazy loading
for cmd in "${NODE_GLOBALS[@]}"; do
eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$@ }"
done
fi |
This comment has been minimized.
This comment has been minimized.
It works fine but vscode tasks does not works with lazy loading |
This comment has been minimized.
In zsh, I have a different setup in my
~/.zshrc
that not only lazyloads nvm and node, but also any globally-installed package