Last active
November 27, 2024 06:55
-
Star
(108)
You must be signed in to star a gist -
Fork
(14)
You must be signed in to fork a gist
-
-
Save fl0w/07ce79bd44788f647deab307c94d6922 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 $@ | |
} |
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
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.
# lazynvm.sh
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
# node.sh
. $(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
It works fine but vscode tasks does not works with lazy loading 👎