-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NVM starting too slow -- can I just add the bin folder to PATH? #782
Comments
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. |
TY 🍻 |
FWIW, I do have a few machines where I rarely use node. I have nvm installed, but don't load it automatically. The few times I need node, I just first do |
@octref By chance are you using |
@ljharb So I just grokked through the zsh doc and enabled the features I need.
I feel the amount of delay omz causes is much more than the delay nvm causes. |
Thanks for the tip. I'd still like to know which |
I have the issue and I'm using bash. |
@benirose what's |
I'm experiencing the Background: I don't really notice the slowness for a single login SSH session, but tools like My
This is on Ubuntu 16.04 (beta 2) server. |
Bash user here, its slow enough to be annoying when starting a new window in tmux:
|
@ljharb sorry I never saw the question you asked. Here's my output for
I installed using Homebrew and have this in my
I have fairly large dotfiles, but when I do
Versus without calling
I do have a default alias set, which I heard slows things down, but I'm not sure how else to configure it:
I hope this helps! |
@benirose Thanks! I've definitely seen both bugs and performance problems when nvm is installed with homebrew (which is unsupported, and the brew formula explicitly says this when installing). Can you try brew uninstalling it, and installing the latest version with the curl command in the readme, and see if you get similar results? |
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. |
For my case, it try to minimal the components init when starting terminal. |
@Tsuki there's no need to do that. just add |
@ljharb not really know what you mean. Where to add --no-use? |
@Tsuki |
@ljharb Thank you, but it still cost around 0.35s. Can it pre-config node like jenv or rbenv with shims. |
For now I'm using @Tsuki's alias nvminit='export NVM_DIR="/Users/mk/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' Pretty good as a workaround... |
@msafi if you add |
@ljharb for some reason that didn't work for me when I tried it earlier. Now it works, and it makes it faster but unfortunately it's still slower than deferring nvm initialization with |
Can |
Not that I know of - it needs to be sourced in the current shell session. |
Oh, turns out if I defer EDIT: looks like with |
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 |
@QinMing as has been stated previously, that won't work for all the other commands you might have in the default version of node (under |
@zanona no, it's not a great solution, and I don't think it should be added to the docs (and there's no need to ping anybody by name, thanks). See #782 (comment) for why it's a bad idea. |
Someone summoned me? J/K @ljharb is doing a great job with this project, he's pretty much in charge of all decisions. |
This comment has been minimized.
This comment has been minimized.
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
|
Another oh-my-zsh user here. I finally have chosen this solution: export PATH=~/.nvm/versions/node/v10.10.0/bin:$PATH
export NVM_DIR="$HOME/.nvm"
. $NVM_DIR/nvm.sh --no-use
. $NVM_DIR/bash_completion It is fast and supports globally installed npm modules. I just need to change the Node version from time to time and make sure it's installed via nvm. |
My solution after reading and testing several ideas and snippets: #1277 (comment) |
A good solution from @pauldraper: Replace nvm stuff in export NVM_DIR="$HOME/.nvm"
nvm_load () {
. $NVM_DIR/nvm.sh
. $NVM_DIR/bash_completion
}
alias node='unalias nvm; unalias node; unalias npm; nvm_load; node $@'
alias npm='unalias nvm; unalias node; unalias npm; nvm_load; npm $@'
alias nvm='unalias nvm; unalias node; unalias npm; nvm_load; nvm $@' |
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. |
https://gist.github.com/audacioustux/4967b2c6d25a004c394455a95f676508#file-nvm-zsh
\. "$NVM_DIR/nvm.sh" --no-use
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
local nvm_default="$(nvm version default)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
unset NPM_CONFIG_PREFIX
if [ $nvmrc_node_version = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ $node_version != $nvm_default ]; then
nvm use default
if [ $nvm_default = "system" ]; then
export NPM_CONFIG_PREFIX=~/.npm-global
else
unset NPM_CONFIG_PREFIX
fi
else
export NPM_CONFIG_PREFIX=~/.npm-global
fi
}
autoload -U add-zsh-hook
add-zsh-hook chpwd load-nvmrc
load-nvmrc
well... it kinda works...
|
@audacioustux what if you have two different versions of the same module, globally installed in two nvm-managed node versions? (also you can |
Thanks for reminding me the no-use part... i've updated the gist for my use case and happy with it 😃 |
Setting |
modified again. added the global module path at the end of $PATH, so the nvm version if installed gets higher precedence. removing the path on every switch kinda feels not good to me.
global node is installed and gets updated by the system package manager (e.g. Pacman, apt) ... so the globally installed modules are shared across node (stable) versions anyway on every update. |
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: ~
➜ time zsh -c 'eval "$(pyenv init -)"; exit'
zsh -c 'eval "$(pyenv init -)"' 0.05s user 0.02s system 101% cpu 0.062 total
~
➜ time zsh -c "source /usr/share/nvm/init-nvm.sh; exit"
zsh -c "source /usr/share/nvm/init-nvm.sh; exit" 0.23s user 0.03s system 112% cpu 0.230 total
~
➜ time zsh -c "source /usr/share/nvm/init-nvm.sh; nvm use 7; exit"
Now using node v7.10.1 (npm v4.2.0)
zsh -c "source /usr/share/nvm/init-nvm.sh; nvm use 7; exit" 0.69s user 0.07s system 112% cpu 0.673 total The NVM equivalent of doing 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. |
@Hubro the only reason it's slow is because The only way this can be sped up is to either a) make If you |
This is (a cleaned up version of) what I use (for bash) and it works for me:
Basically, this just uses some Bash path manipulation to put the latest node version's This reduced the startup time for a new shell by more than one second on average on my machine. |
I changed to |
I prefer
|
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. |
https://gist.github.com/audacioustux/4967b2c6d25a004c394455a95f676508#file-nvm-zsh |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@ljharb I was wondering if there really wasn't something I could do so nvm would load faster (and check for all you have to do is source note: it uses fast-nvm.sh locate_nvmrc() {
locate -w .nvmrc | xargs -I'{}' realpath --relative-to=. '{}' | awk '/^(\.\.\/)*\.nvmrc$/ {print $0}' | sort | xargs -I'{}' realpath '{}'
}
export PATH="$HOME/.nvm/versions/node/$(/bin/cat $HOME/.nvm/alias/default)/bin:$PATH"
nvm() {
. $HOME/.nvm/nvm.sh; nvm "$@"
}
DEFAULT=$(locate_nvmrc)
cd() {
if builtin cd "$@" 2>/dev/null; then
FOUND="$(locate_nvmrc)"
if [ "$DEFAULT" != "$FOUND" ]; then
DEFAULT=$FOUND
nvm use
fi
else
if test -f "$@"; then echo "cd: not a directory: $*"
elif test -d "$@"; then echo "cd: can not change to $*"
else echo "cd: no such file or directory: $*"
fi
return 1
fi
} |
If your |
Ok, not familiar with nvm codebase, but i'll try to see if I can create a pr. Also trying to do some optimizations, see how fast can this goes. Like I said before, this is using edit: in the project readme there is a section about "Calling |
Hey. I use a minimal zsh config to make sure my shell starts fast.
However after adding
The shell starts much slower.
So I changed it to
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?
The text was updated successfully, but these errors were encountered: