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

Node.js: avoid slow terminal startup time due to nvm #978

Merged
merged 12 commits into from
Sep 29, 2021

Conversation

cycool29
Copy link
Contributor

@cycool29 cycool29 commented Sep 28, 2021

Using nvm to install nodejs is terrible for me. It slowed down my terminal loading time (there are few issues about this: nvm-sh/nvm#1277, nvm-sh/nvm#1261). Although there are many solution on the web like editing .bashrc, modifying nvm install script, etc, I don't want to make it so confusing...

Then Nodesource does it perfectly.

  • It won't slow down terminal loading speed.
  • It will make pi-apps easy to update Node.js with only changing the nodesource link, but with nvm, pi-apps will probably hard to update as it need to find out what version of nodejs user have, what is the latest version does nvm have, etc.
  • Also, Node.js from nodesource there is a proper way to uninstall with just sudo apt purge nodejs and remove source list and apt-key. On nvm, you will need to delete the folders one-by-one.

However, the snap one is also good, but I prefer nodesource since it don't have any dependencies.

@Itai-Nelken
Copy link
Contributor

The version from the nodesource repo was used, but it bricked some systems including mine (broke permissions) so we changed to nvm.

@cycool29
Copy link
Contributor Author

The version from the nodesource repo was used, but it bricked some systems including mine (broke permissions) so we changed to nvm.

How about using the snap one? I don't really think nvm is a good choice (especially in terminal loading speed).

I tried other method like nvs, nodenv, fnm, all will slow down terminal speed as they will need to source their script before launching the terminal. But n works pretty well, it won't slow down terminal speed. However, snap and nodesource is still best for me.

@cycool29
Copy link
Contributor Author

@Botspot What do you think?

@Botspot
Copy link
Owner

Botspot commented Sep 29, 2021

How about using the snap one? I don't really think nvm is a good choice (especially in terminal loading speed).

The slow terminal start-up time from nvm bothers me too. But it's reliable. We commonly receive snap error reports; we very rarely receive Node.js error reports. It would be a pity to switch to a less reliable installation method - sure it would benefit some users and their terminal start-up time, but it would completely break node-js on other user's systems.

So, while I'm open to a better alternative, I don't believe snap is the right option due to its reliably issues.

@cycool29
Copy link
Contributor Author

cycool29 commented Sep 29, 2021

Since nodesource and snap is not reliable and stable, I am searching for ways to increase nvm initialize time.
Finally, this works for me:

# Defer initialization of nvm until nvm, node or a node-dependent command is
# run. Ensure this block is only run once if .bashrc gets sourced multiple times
# by checking whether __init_nvm is a function.
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
fi

I got it from here : https://www.growingwiththeweb.com/2018/01/slow-nvm-init.html

I will add to the script now.

@cycool29
Copy link
Contributor Author

cycool29 commented Sep 29, 2021

Done. @Itai-Nelken @Botspot
Tested installing & uninstalling in pi-apps. The terminal load faster now!

@Botspot Botspot merged commit 06674ec into Botspot:master Sep 29, 2021
@Botspot Botspot changed the title Use Nodesource to install Node.js Node.js: avoid slow terminal startup time due to nvm Sep 29, 2021
@cycool29 cycool29 deleted the nodejs branch September 29, 2021 23:11
@Itai-Nelken
Copy link
Contributor

This is a good idea, the problem is that there are much more node dependant commands, and adding all of them makes no sense.
I think we should give the user the choice to add this, maybe someone uses a lot of node stuff and doesn't care how fast bash starts as long as he can use all of them?

@Botspot
Copy link
Owner

Botspot commented Oct 1, 2021

This is a good idea, the problem is that there are much more node dependant commands, and adding all of them makes no sense. I think we should give the user the choice to add this, maybe someone uses a lot of node stuff and doesn't care how fast bash starts as long as he can use all of them?

I did consider this factor, but figured that if someone asks for help we can solve it on a case-by-case basis, reconsidering as necessary going forward.

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

Successfully merging this pull request may close these issues.

3 participants