While playing around with zsh frameworks (I ended up using zgen) I discovered that nvm is the slowest part of my shell startup. I don't actually know bash but somehow made this hack to speed it up, which helped me think of a better mechanism for lazy loading nvm:
$PATH$PATH segment before exiting. I cached the $PATH segment into a self-contained shell script, named use_cached_nvm_bins.sh, but maybe symlinks could be used instead of caching to a file.nvm$PATH and only loads the bulk of the code when nvm is invoked. In my hack I was so lazy I didn't even load nvm, and wrote a load_nvm function instead.This is related to/a duplicate of #709.
Can you elaborate on option 1? I'm not sure what benefit this would offer, as the PATH might be different each time you load a shell, or nvm use.
As for option 2, while you certainly could do something like this:
nvm() {
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
nvm $@
}
… the complexity of adding that to the install script, which already has issues even adding the single "source" line (see https://github.com/creationix/nvm/labels/installing%3A%20profile%20detection), worries me a lot.
Why would the PATH be different each time you load a shell? After each call to nvm use this cache would need to be updated. Sounds like nvm needs some work, more test coverage, etc.
nvm() {
[ -s "$NVM_DIR/nvm_CORE.sh" ] && . "$NVM_DIR/nvm_CORE.sh" # This loads nvm
nvm $@
}
I have multiple shells open at once, and I could easily change the PATH command in my shell profile after loading one shell tab, but before opening another one. nvm is designed to be used in multiple shells at once so any "cache" would have to be per-shell-session - which is what $PATH already is.
That's a good caveat to this design.
I'll leave this open as a "lazy load" feature request, for sure.
For record, my hack: https://github.com/devinrhode2/dotfiles/blob/master/.zshrc#L28-L53
Hey, lazy loading nvm in this way drops startup for new shells from a second to half a second, which is great! But npm is not defined until nvm is sourced, so I end up doing nvm ls or something similar before I can run any npm command.
Suggestions on how to source nvm under the hood for npm commands as well? (as you can probably tell, my shell (or zsh in this case) skills are nonexistent)
I added this function, which seems to work well enough 😄
Suggestions appreciated!
That approach will still only work for whichever things you hardcode - node, npm, etc - but will not work for any global modules you have installed in a generic fashion.
node and npm are the only two commands I run globally often, the only other thing I have installed globally is typings, which is run very rarely, so that's no problem!
And as it halves the time it takes to open a new shell, I think it's worth it. Duplicating that sort of function isn't much work if I need to either.
But that's still the "suggested" way to load nvm lazily?
Officially the suggestion is to not load nvm lazily, and to either tolerate the time it takes to load, or write me a PR to make it faster ;-)
However, the approach in your example is fine if you are aware of the caveats, and if you'll remember that you've done that when debugging any issues that pop up in the future.
SimenB my load_nvm function is pretty fragile but it basically does what you're looking for, it caches the paths to the loaded npm and node binaries by writing them to a file, and then I have that file sourced in my zshrc, so then future shells get npm and node added to the PATH without loading nvm. It's really broken because, like you, I don't know bash.. I just hacked together some snippits from SO.. But it works and can be improved by learning a wee bit of bash..
is there any final solution?
@carlesba https://github.com/creationix/nvm/issues/730#issuecomment-226949107
For anyone here using zsh interested in lazy loading I've just added support for lazy loading in zsh-nvm: https://github.com/lukechilds/zsh-nvm#lazy-loading
I'm getting about 70x faster startup times (874ms down to 12ms)
lukechilds' plugin is cool.
Most helpful comment
For anyone here using zsh interested in lazy loading I've just added support for lazy loading in
zsh-nvm: https://github.com/lukechilds/zsh-nvm#lazy-loadingI'm getting about 70x faster startup times (874ms down to 12ms)