Skip to content

Instantly share code, notes, and snippets.

@fl0w fl0w/
Last active Mar 27, 2020

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 $@

This comment has been minimized.

Copy link

christophemarois commented Jul 17, 2018

In zsh, I have a different setup in my ~/.zshrc that not only lazyloads nvm and node, but also any globally-installed package

# Add every binary that requires nvm, npm or node to run to an array of node globals
NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)

# Lazy-loading nvm + npm on node globals call
load_nvm () {
  export NVM_DIR=~/.nvm
  [ -s "$(brew --prefix nvm)/" ] && . "$(brew --prefix nvm)/"

# Making node global trigger the lazy loading
for cmd in "${NODE_GLOBALS[@]}"; do
  eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$@ }"

This comment has been minimized.

Copy link
Owner Author

fl0w commented Oct 23, 2018

updated for CLI npx and bash completion


This comment has been minimized.

Copy link

nicholasmakhija commented Dec 5, 2019

Thanks @christophemarois, I've been hunting for a solution like this one!

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.