Skip to content
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

Closed
octref opened this issue Jul 9, 2015 · 72 comments
Closed

NVM starting too slow -- can I just add the bin folder to PATH? #782

octref opened this issue Jul 9, 2015 · 72 comments

Comments

@octref
Copy link

@octref octref commented Jul 9, 2015

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?

@creationix
Copy link
Collaborator

@creationix creationix commented Jul 9, 2015

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.

@octref
Copy link
Author

@octref octref commented Jul 9, 2015

TY 🍻

@octref octref closed this Jul 9, 2015
@creationix
Copy link
Collaborator

@creationix creationix commented Jul 9, 2015

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 . ~/nvm/nvm.sh before using nvm or node for that session.

@ljharb
Copy link
Member

@ljharb ljharb commented Jul 9, 2015

@octref By chance are you using oh-my-zsh? I've noticed that the majority of performance complaints are omz users, and past investigation makes me strongly suspect some of the options it sets causing slowness.

@octref
Copy link
Author

@octref octref commented Jul 9, 2015

@ljharb
I once tried to eliminate oh-my-zsh's slowness but failed. It's too bloated.

So I just grokked through the zsh doc and enabled the features I need.

# -------------------------------- #
# Zsh
# -------------------------------- #

fpath=(/usr/local/share/zsh-completions /usr/local/share/zsh/site-functions $fpath)

autoload -Uz compinit colors add-zsh-hook
colors
compinit

# Colored auto completion
zstyle ':completion:*' list-colors "di=34"
# Case insensitive completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'

# Stop zsh eating space before my pipe chars
export ZLE_REMOVE_SUFFIX_CHARS=''

# Allow extended glob like ^file, *~file (everything except file)
setopt extended_glob

# -------------------------------- #
# Setup prompt
# -------------------------------- #

set_prompt() {
  local ICON=''
  # Check if in a git folder
  if [ -d '.git' ] || [[ $(command git rev-parse --is-inside-work-tree 2>/dev/null) == "true" ]]; then
    # Check for uncommited changes, ignore untracked files
    if [[ -n $(command git status --porcelain --untracked-files=no | tail -1) ]]; then
      ICON="%{$fg[red]%}>"
    else
      ICON="%{$fg[green]%}>"
    fi
  else
    ICON='>'
  fi
  PROMPT=" %{$fg[blue]%}%1~ $ICON%{$reset_color%} "
}

add-zsh-hook precmd set_prompt

I feel the amount of delay omz causes is much more than the delay nvm causes.
I think you can direct those omz users to https://github.com/sindresorhus/pure . Simple and fast zsh configuration.

@ljharb
Copy link
Member

@ljharb ljharb commented Jul 9, 2015

Thanks for the tip. I'd still like to know which zsh option is causing the problem, since sourcing nvm.sh is lightning fast in stock zsh as well as every other shell I've tested. If the code you posted is all you're including beyond stock zsh then thanks, that's awesome, and I'll take a deeper look once I'm back in my country in a week and a half :-)

@benirose
Copy link

@benirose benirose commented Apr 1, 2016

I have the issue and I'm using bash.

@ljharb
Copy link
Member

@ljharb ljharb commented Apr 1, 2016

@benirose what's nvm debug output?

@jamesvl
Copy link

@jamesvl jamesvl commented Apr 10, 2016

I'm experiencing the nvm slowdown in bash as well.

Background: I don't really notice the slowness for a single login SSH session, but tools like vagrant rsync will do about 12 SSH sessions in quick succession, and when each takes ~1s vs. being nearly instant, it slows down file syncing by an order of magnitude.

My nvm debug output:

nvm --version: v0.31.0
$SHELL: /bin/bash
$HOME: /home/vagrant
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: v5.10.1
which node: $NVM_DIR/versions/node/v5.10.1/bin/node
which iojs:
which npm: $NVM_DIR/versions/node/v5.10.1/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v5.10.1
npm root -g: $NVM_DIR/versions/node/v5.10.1/lib/node_modules

This is on Ubuntu 16.04 (beta 2) server.

@ghost
Copy link

@ghost ghost commented Jun 22, 2016

Bash user here, its slow enough to be annoying when starting a new window in tmux:

time /usr/local/opt/nvm/nvm.sh

real    0m0.617s
user    0m0.442s
sys 0m0.204s
@benirose
Copy link

@benirose benirose commented Aug 11, 2016

@ljharb sorry I never saw the question you asked. Here's my output for nvm debug.

$ nvm debug
nvm --version: v0.30.1
$SHELL: /bin/bash
$HOME: /Users/benrose
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: v6.3.1
which node: $NVM_DIR/versions/node/v6.3.1/bin/node
which iojs: 
which npm: $NVM_DIR/versions/node/v6.3.1/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v6.3.1
npm root -g: $NVM_DIR/versions/node/v6.3.1/lib/node_modules
-bash: [: : integer expression expected

I installed using Homebrew and have this in my .bash_profile

export NVM_DIR=~/.nvm
. $(brew --prefix nvm)/nvm.sh

I have fairly large dotfiles, but when I do time source ~/.bash_profile with the line I get:

real    0m1.663s
user    0m0.972s
sys 0m0.596s

Versus without calling nvm.sh

real    0m0.359s
user    0m0.228s
sys 0m0.133s

I do have a default alias set, which I heard slows things down, but I'm not sure how else to configure it:

$ nvm list
       v0.10.41
         v4.4.6
         v4.4.7
->       v6.3.1
default -> 6 (-> v6.3.1)
node -> stable (-> v6.3.1) (default)
stable -> 6.3 (-> v6.3.1) (default)
iojs -> N/A (default)

I hope this helps!

@ljharb
Copy link
Member

@ljharb ljharb commented Aug 11, 2016

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

@benirose
Copy link

@benirose benirose commented Aug 11, 2016

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.

@Tsuki
Copy link

@Tsuki Tsuki commented Sep 7, 2016

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.

@ljharb
Copy link
Member

@ljharb ljharb commented Sep 7, 2016

@Tsuki there's no need to do that. just add --no-use to the line that does the sourcing, and there's no need to defer it.

@Tsuki
Copy link

@Tsuki Tsuki commented Sep 7, 2016

@ljharb not really know what you mean. Where to add --no-use?

@ljharb
Copy link
Member

@ljharb ljharb commented Sep 7, 2016

@Tsuki . "$(brew --prefix nvm)/nvm.sh" --no-use - although I highly discourage using brew at all, as it's completely unsupported.

@Tsuki
Copy link

@Tsuki Tsuki commented Sep 7, 2016

@ljharb Thank you, but it still cost around 0.35s. Can it pre-config node like jenv or rbenv with shims.
So that need not to load nvm.sh every time.

@msafi
Copy link

@msafi msafi commented Sep 10, 2016

nvm is 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 nvminit solution like this:

alias nvminit='export NVM_DIR="/Users/mk/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"'

Pretty good as a workaround...

@ljharb
Copy link
Member

@ljharb ljharb commented Sep 10, 2016

@msafi if you add --no-use after . "$NVM_DIR/nvm.sh"', is it still slow?

@msafi
Copy link

@msafi msafi commented Sep 10, 2016

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

@msafi
Copy link

@msafi msafi commented Sep 10, 2016

Can nvm initialization be run in a background process without interrupting the user? (just a thought, I don't know much about shells)

@ljharb
Copy link
Member

@ljharb ljharb commented Sep 10, 2016

Not that I know of - it needs to be sourced in the current shell session.

@msafi
Copy link

@msafi msafi commented Sep 10, 2016

Oh, turns out if I defer nvm initialization, my global Node CLI commands don't work until I nvminit, so I guess --no-use is the best solution for now.

EDIT: looks like with --no-use I don't have access to npm and node at start-up. I have to do something like nvm use stable before I can begin using them. So, that's a no-go for me either...back to square one...

@QinMing
Copy link

@QinMing QinMing commented Sep 11, 2016

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

@ljharb
Copy link
Member

@ljharb ljharb commented Sep 11, 2016

@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 npm ls -g --depth=0).

@netvisao
Copy link

@netvisao netvisao commented Feb 8, 2018

W/o going too much into introspecting the nvm script, I looked into lazy loading
Here is how my ~/.bash_profile looks like

# NPM
 export PATH=$PATH:~/.npm/node_modules/.bin # Global modules
 export NVM_DIR=$HOME/.nvm
 nvm_load () { . $NVM_DIR/nvm.sh && . $NVM_DIR/bash_completion; }
 alias node='unalias node; nvm_load; node $@'
 alias npm=' unalias npm;  nvm_load; npm  $@'

The slowdown will only be noticed when node or npm is executed the first time during the current terminal session

@ljharb
Copy link
Member

@ljharb ljharb commented Feb 9, 2018

@netvisao see #782 (comment) for why that's undesirable.

@netvisao
Copy link

@netvisao netvisao commented Feb 9, 2018

@ljharb the first time node or npm runs, nvm is guaranteed to run, and before nvm is running, the node and npm binaries are unaliased to their system defaults. The aliases should be taking care of that.

@ljharb
Copy link
Member

@ljharb ljharb commented Feb 9, 2018

@netvisao right, but say you have eslint installed as a global binary in your nvm-managed node; eslint won't work until after node is ran, which is not desirable.

@giggio
Copy link

@giggio giggio commented May 9, 2018

Great solution from @netvisao. I just changed it to unalias both npm and node:

nvm_load () { . $NVM_DIR/nvm.sh && . $NVM_DIR/bash_completion; }
alias node='unalias node; unalias npm; nvm_load; node $@'
alias npm='unalias node; unalias npm; nvm_load; npm $@'
@zanona
Copy link

@zanona zanona commented Jul 1, 2018

@netvisao solution is great and should be added to the docs! It just feels like it's the right thing to do :)
@creationix what do you reckon?

@ljharb
Copy link
Member

@ljharb ljharb commented Jul 2, 2018

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

@creationix
Copy link
Collaborator

@creationix creationix commented Jul 27, 2018

Someone summoned me? J/K @ljharb is doing a great job with this project, he's pretty much in charge of all decisions.

@rpeshkov

This comment was marked as spam.

@nikhilw
Copy link

@nikhilw nikhilw commented Oct 6, 2018

I had wrapped the nvm's startup into a function which I would just invoke manually.

function loadNVM() {
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
}

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 list for example, without first running node/npm. So here is their solution combined, with one line added:

export PATH=$PATH:~/.npm/node_modules/.bin # Global modules
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 $@'
@gimenete
Copy link

@gimenete gimenete commented Jan 19, 2019

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.

@craftey
Copy link

@craftey craftey commented Feb 3, 2019

My solution after reading and testing several ideas and snippets: #1277 (comment)

@reduardo7
Copy link

@reduardo7 reduardo7 commented Feb 21, 2019

A good solution from @pauldraper:

Replace nvm stuff in ~/.bashrc with:

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 $@'
@ljharb
Copy link
Member

@ljharb ljharb commented Feb 21, 2019

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.

@audacioustux
Copy link

@audacioustux audacioustux commented Feb 3, 2020

https://gist.github.com/audacioustux/4967b2c6d25a004c394455a95f676508#file-nvm-zsh

$HOME/.zsh/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

.zshrc

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && source $HOME/.zsh/nvm.zsh

well... it kinda works...

  • system should be default nvm alias default system
  • globally installed modules are accessible, in or out nvm, but nvm version gets more precedence; @ljharb
  • no need for alias
  • only triggerd by .nvmrc file
@ljharb
Copy link
Member

@ljharb ljharb commented Feb 4, 2020

@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-use without incurring any speed penalty, and then you don't need to copy-paste any of nvm's functions)

@audacioustux
Copy link

@audacioustux audacioustux commented Feb 4, 2020

@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-use without incurring any speed penalty, and then you don't need to copy-paste any of nvm's functions)

Thanks for reminding me the no-use part... i've updated the gist for my use case and happy with it 😃

@ljharb
Copy link
Member

@ljharb ljharb commented Feb 4, 2020

Setting NPM_CONFIG_PREFIX is a very bad idea; global modules must never be shared across node versions.

@audacioustux
Copy link

@audacioustux audacioustux commented Feb 4, 2020

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.

must never be shared across node versions

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.
I primarily need nvm for development purposes. so .nvmrc & devDependencies is there to save me most of the time. and.. well... not the best solution but it works 😩

@Hubro
Copy link

@Hubro Hubro commented Feb 19, 2020

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 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.

@ljharb
Copy link
Member

@ljharb ljharb commented Feb 19, 2020

@Hubro the only reason it's slow is because nvm use invokes npm config get prefix, which is slow.

The only way this can be sped up is to either a) make npm faster, or b) replicate npm's logic inside nvm, which is both complex, and a brittle approach.

If you nvm unalias default, or source nvm with --no-use, then there will be no performance impact (but you'll have to nvm use when you need node/npm).

byronsanchez added a commit to byronsanchez/dotfiles that referenced this issue Apr 25, 2020
Lazy-Load NVM - Sourcing NVM directly takes about 0.5s, which actually
slows down terminal initialization and is noticeable. I'll try different
permutations if I encounter different roadblocks due to the use of
lazy-loading. Otherwise, I'll stick to the first thing that works and is
easier to reason about.

Links:

- nvm-sh/nvm#782
- https://www.growingwiththeweb.com/2018/01/slow-nvm-init.html
- nvm-sh/nvm#1277 (comment)
- https://gist.github.com/gfguthrie/9f9e3908745694c81330c01111a9d642
- https://www.reddit.com/r/node/comments/4tg5jg/lazy_load_nvm_for_faster_shell_start/d5ib9fs/
@dpryden
Copy link

@dpryden dpryden commented Apr 27, 2020

This is (a cleaned up version of) what I use (for bash) and it works for me:

export NVM_DIR="$HOME/.nvm"
# These are the Node versions installed by nvm. Add the latest one to the PATH by default.
node_versions=("$NVM_DIR"/versions/node/*)
if (( "${#node_versions[@]}" > 0 )); then
    PATH="$PATH:${node_versions[$(("${#node_versions[@]}" - 1))]}/bin"
fi
if [[ -s "$NVM_DIR/nvm.sh" ]]; then
    # Shim out `nvm` with a function that loads the real `nvm` on first use.
    # Since we already updated our PATH earlier, we don't need to actually load nvm
    # to get "node" and related binaries to work.
    nvm() {
        # shellcheck disable=SC1090
        source "$NVM_DIR/nvm.sh"
        nvm "$@"
    }
fi
if [[ -s "$NVM_DIR/bash_completion" ]]; then
    # shellcheck disable=SC1090
    source "$NVM_DIR/bash_completion"
fi

Basically, this just uses some Bash path manipulation to put the latest node version's bin directory on the path, and then adds a Bash function to lazy-load nvm for when I want to switch versions.

This reduced the startup time for a new shell by more than one second on average on my machine.

@giggio
Copy link

@giggio giggio commented Apr 27, 2020

I changed to n, now I don't need to load anything on startup.

@craftey
Copy link

@craftey craftey commented Apr 27, 2020

I changed to n, now I don't need to load anything on startup.

I prefer nvm over n for 4 reasons:

  1. With n you cannot have different node versions in two shell sessions at the same time, AFAIK.
    This is a common situation if your work on multiple projects in parallel.
  2. With n one cannot load a specific node version depending on a file .nvmrc in current folder, which is very useful if you have multiple projects with different node versions.
  3. Switching node version with n took ages when I tested last time.
  4. n is a shitty name for a tool.
@giggio
Copy link

@giggio giggio commented Apr 27, 2020

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
Copy link

@audacioustux audacioustux commented Apr 28, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.