Home > Mobile >  Difference among ruby gems, npm packages, and yarn
Difference among ruby gems, npm packages, and yarn

Time:09-07

When I start a new Rails project, it gives me a template with a few different files for packages: Gemfile, package.json, and yarn.lock. I'm trying to understand if all three of these packages work together, or if I'm supposed to choose one. For example, in my Gemfile, I can add 'jquery-rails' but is this the same as installing jQuery to my package.json file? I'm having trouble understanding the difference between running bundle install and npm install.

Thanks.

CodePudding user response:

In short it looks like this:

In Ruby, the bundler analyzes the list of libraries (gems) specified in the Gemfile, builds a dependency tree and fixes it in Gemfile.lock, downloads gems from different sources

In JS, various managers do the same: npm, yarn, bower, etc. By default Rails use yarn. They download all libraries to node_modules folder

package.json is analogue of Gemfile (but more advanced, for example you can define command line scripts there). yarn.lock is analogue of Gemfile.lock

Thus, bundler for Ruby and yarn / npm for JS libraries

CodePudding user response:

Gemfile is to manage ruby gems, while package.json and yarn.lock for js packages.
The good thing about installing jQuery via jquery-rails is that will allow you to easily integrate jQuery into a Rails project, regardless what asset manager you're using it.
On the other hand, installing jQuery using npm/yarn allows you to upgrade the package directly from the original repo, without having an intermediary (jquery-rails in this case) that eventually could have the gem unattended (as now that they're using jquery 3.6.0 while the last one is 3.6.1).
Personally, I'd choose the 2nd option, mostly because in general is a good practice to have these js/ruby worlds working separately, but the other reason is because any other js library you eventually will need it, probably will follow the same steps as installing jQuery, so it's not really a big deal integrating it with Rails in this way.

  • Related