I Found a Solution for "nvm" Slowing The Bash Initialisation

March 18, 2021nvmnodenpmbashtip slips

nvm Is Taking Too Much Time

You don't know nvm? it's a node version manager basically you can modify easily which node version you wanna use.

I do a lot of thing with vim in terminal and by having nvm it slows my terminal opening, but it's not going to shit on me every now and then bacause I have a solution for it. So here is the Fix I found on internet.


By default your .bashrc will include this command in it right after you installing nvm

 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

Thats the problem, here is how to tackle it. Replace those line with

 if [ -s "$HOME/.nvm/nvm.sh" ] && [ ! "$(type -t __init_nvm)" = function ]; then
  export NVM_DIR="$HOME/.nvm"
  [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
  declare -a __node_commands=('nvm' 'node' 'npm' 'yarn' 'gulp' 'grunt' 'webpack')
  function __init_nvm() {
    for i in "${__node_commands[@]}"; do unalias $i; done
    . "$NVM_DIR"/nvm.sh
    unset __node_commands
    unset -f __init_nvm
  for i in "${__node_commands[@]}"; do alias $i='__init_nvm && '$i; done

I don't know what the fuck is the above script doing, but that solve my problem. I found it on internet tho. Here is the LINK TO AUTHOR OF THE SOLUTION