jorgebucaran / nvm.fish

The Node.js version manager you'll adore, crafted just for Fish
https://git.io/nvm.fish
MIT License
2.06k stars 69 forks source link

Add support for nvm style default packages #178

Closed kallja closed 2 years ago

kallja commented 2 years ago

nvm has support for Default Packages, where one can define a set of global packages that are installed during each install of a node version.

I'd love to see this introduced into nvm.fish. I frequently need to have yarn, for example, installed into my fresh node environments.

In fact, I already implemented this for myself. A PR is soon to follow for your consideration.

jorgebucaran commented 2 years ago

Thanks, @kallja. What's the expected behavior when you switch to a different version or uninstall said version? What happens when you uninstall nvm itself?

kallja commented 2 years ago

The proposed functionality should only affect what happens on install of a Node version. If there are default packages defined, those packages will be installed (using npm) after the Node version has been installed and activated. Installing a global Node module this way is exactly the same as installing that module manually using npm install -g MODULE_NAME after nvm install VERSION.

Added benefit of this approach vs explicit installation in (automation scripts) is that nvm only calls npm install when a new Node version is installed. Thanks to great performance, requests to install pre-existing Node versions run quickly without taking unnecessary actions.

As to the specific questions, this proposed behavior doesn't change the current behavior in any way.

  1. Switching to different Node version just switches to that version. Global npm modules will be what they were when that version was last worked with.
  2. Uninstalling a Node version uninstalls its entire environment, including any global modules installed for it whether installed automatically during install or manually afterwards.
  3. Uninstalling nvm removes any and all Node installations installed by it, including their global modules.
jorgebucaran commented 2 years ago

Thank you for all the info, @kallja. I'd like to propose an alternative solution to this problem via Fish events.

Say we fire an nvm_node_install event whenever a Node version is installed for the first time. Then you can npm install or react to this event in any way you like. For example, in config.fish:

function npm_install_default_packages --on-event nvm_node_install
    npm install --global yarn
end

What do you think?

jorgebucaran commented 2 years ago

I went ahead and decided to add this. 🎉 Thank you, @kallja!

kallja commented 2 years ago

I'm glad I was able to help out one tiny bit. Thank you for including this change!