nvm: NVM starting too slow -- can I just add the bin folder to PATH?
Hey. I use a minimal zsh config to make sure my shell starts fast. However after adding
. ~/.nvm/nvm.sh
The shell starts much slower.
So I changed it to
PATH=~/.nvm/versions/io.js/v2.3.3/bin:$PATH
I know I can’t utilize nvm full this way, but I’m OK with just using one version. When I want to use another version I’ll load ~/.nvm/nvm.sh to use nvm and then change the node bin PATH.
But will this setting break anything? Any ENV var I should set up manually?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 12
- Comments: 80 (24 by maintainers)
Commits related to this issue
- Replace brew nvm, to gain shave down brew nvm slowdowns https://github.com/creationix/nvm/issues/782#issuecomment-255560894 — committed to floscr/old-vimrc-dotfiles by floscr 8 years ago
I made my zsh load 0.8 seconds faster by loading nvm when “nvm”, “npm” or “node” is used for the first time. Check out my Gist https://gist.github.com/QinMing/364774610afc0e06cc223b467abe83c0#file-zshrc-L15-L37
Thanks for working on this @lxe! this has been slowing me down for at least the last 9 months and I didn’t even realize what was causing the 1.5s delay in spinning up another terminal.
I came to this workaround after reading what @krokofant put:
The brilliant part is that you can get by even if you totally forget about the workaround,
nodewill just start working after the first time you type nvm and you think “huh, that’s odd” and move on with life 😃I had wrapped the nvm’s startup into a function which I would just invoke manually.
Solution from @netvisao 's and @giggio 's modification, does the same but better, no manual load required. But it still didnt work when I just say
nvm listfor example, without first running node/npm. So here is their solution combined, with one line added:Bash user here, its slow enough to be annoying when starting a new window in tmux:
@msafi if you add
--no-useafter. "$NVM_DIR/nvm.sh"', is it still slow?For my case, it try to minimal the components init when starting terminal. I have add
alias nvminit='. "$(brew --prefix nvm)/nvm.sh"'as my workaround.Great solution from @netvisao. I just changed it to unalias both npm and node:
I have the issue and I’m using bash.
W/o going too much into introspecting the nvm script, I looked into lazy loading Here is how my
~/.bash_profilelooks likeThe slowdown will only be noticed when
nodeornpmis executed the first time during the current terminal sessionA good solution from @pauldraper:
Replace nvm stuff in
~/.bashrcwith:The two things that are slowing down the nvm.sh invocation (which automatically runs
nvm use):npm config get prefixwhich invokes, node/npm delaying things up to 2 secondsI’m going to take a crack at these perf issues one by one
@Hubro the only reason it’s slow is because
nvm useinvokesnpm config get prefix, which is slow.The only way this can be sped up is to either a) make
npmfaster, or b) replicate npm’s logic insidenvm, which is both complex, and a brittle approach.If you
nvm unalias default, or sourcenvmwith--no-use, then there will be no performance impact (but you’ll have tonvm usewhen you need node/npm).I just put together a solution to that defers initialization of nvm until it’s needed that works pretty well:
I go into more detail in this blog post: http://www.growingwiththeweb.com/2018/01/slow-nvm-init.html
It’s a bit hacky but this is what I use
So hey, why is this issue closed?
Also, after reading through all the comments here, I don’t really see anybody discussing why nvm is so slow to initialize in the first place. Other version managers (like pyenv and rvm) are vastly faster than nvm to initialize.
Quick comparison with pyenv:
The NVM equivalent of doing
eval "$(pyenv init -)"takes over 34 times longer to execute.Is there nothing that can be done to speed up nvm, rather than devising workarounds like lazy loading?
Personally I find the delay when starting my terminal unacceptable, so I’ve switched to manually enabling nvm when I need it. I wish that wasn’t necessary.
@zeroby0 This is what I currently have and it’s working fine for me.
I just run
source ~/.nvm/nvm.shwhen I actually need nvm.@ljharb tks. now i use
zsh-nvm,this is a great plugin.Oh, turns out if I defer
nvminitialization, my global Node CLI commands don’t work until Invminit, so I guess--no-useis the best solution for now.EDIT: looks like with
--no-useI don’t have access tonpmandnodeat start-up. I have to do something likenvm use stablebefore I can begin using them. So, that’s a no-go for me either…back to square one…Someone summoned me? J/K @ljharb is doing a great job with this project, he’s pretty much in charge of all decisions.
@netvisao right, but say you have
eslintinstalled as a global binary in your nvm-managed node;eslintwon’t work until afternodeis ran, which is not desirable.The zsh-nvm plugin supports lazy load and solved this issue for me. My load time dropped from 2.2s to 0.8s.
@Tsuki
. "$(brew --prefix nvm)/nvm.sh" --no-use- although I highly discourage using brew at all, as it’s completely unsupported.I just did some other digging and saw the homebrew thing. That’s a total bummer, and while I can understand the need to limit support scopes, the most popular package manager for mac shouldn’t be excluded.
I’ll give it a try to uninstall and install the old fashioned way and let you know if this helps speed things up at all.
https://gist.github.com/audacioustux/4967b2c6d25a004c394455a95f676508#file-nvm-zsh with this, it takes less than one second, and version change is only triggered by .nvmrc, and use the system node version by default
It makes sense to optimize for your demands. I don’t change Node.js versions that often, maybe once every few days. But I open several terminals a day, so the startup time impacts me more. Switching could be faster, but I can easily live with it. And I don’t remember the last time I needed 2 node versions on different shell sessions, but this is clearly the main difference: nvm touches your session, not your system, and that takes time. N, on the other hand, changes the system, and the session loads faster. And yes, N sucks as a name. You can’t search for it.
@audacioustux what if you have two different versions of the same module, globally installed in two nvm-managed node versions?
(also you can
. $NVM_DIR/nvm.sh --no-usewithout incurring any speed penalty, and then you don’t need to copy-paste any of nvm’s functions)Again, note that this has the caveat of not allowing globally installed modules in the default node to work prior to invoking node/npm/nvm.
@TheNotary however, that means that both
nodeandnpm, as well as any globally installed binaries you have in the default node version, will be unavailable until the first time you’ve typed annvmcommand.@gatspy you’d then have to manually run
nvm usewhen you were ready to use node.nvmis also causing my shell start-up time to be slow. Yes, I’m using oh-my-zsh. Happy to provide any additional information.For now I’m using @Tsuki’s
nvminitsolution like this:Pretty good as a workaround…
Thanks for the tip. I’d still like to know which
zshoption is causing the problem, since sourcingnvm.shis lightning fast in stockzshas well as every other shell I’ve tested. If the code you posted is all you’re including beyond stockzshthen thanks, that’s awesome, and I’ll take a deeper look once I’m back in my country in a week and a half 😃As far as I know, that will work fine. You seem to understand that the nvm command won’t be usable without first sourcing it.