Denis Pshenov
Denis Pshenov

Reputation: 11327

bundle vs npm approach or why does bundle/gem installs globally?

I am confused by the Bundle/Gem approach of global dependencies. I thought global dependencies are a thing of a past. I present to you two approaches of handling dependencies, NPM vs Bundle/Gem.

NPM

Let me demonstrate the power of NPM. Say you have an app with the following folder structure:

/app
  /admin
     package.json
  /front-end
     package.json

You can see that inside my app I have two other mini-apps. Each of those has its own package.json.

Say both of them have q as a requirement, but with different version. Running npm install in both of these folders will install appropriate versions to node_modules of each mini app. Everything is nicely isolated and works very well.

Bundle/Gem

Bundles approach is slightly different. You use Gemfile to specify your dependencies. After running bundle install the gems will be put into a system wide location. That means that if I have an app that requires different versions of a gem within its mini-apps then it wont work.

Work arounds that make no sense to me:

I am 100% sure I'm missing something very trivial here. Please point me into the right direction.

Upvotes: 19

Views: 5364

Answers (2)

ian
ian

Reputation: 12251

If you use bundle install --binstubs path=vendor then you get locally (to the project) installed gems, not system wide. Bundler still takes a different approach to libraries that it needs yet have different versions, by installing one that is compatible to both.

Upvotes: 2

Denis Pshenov
Denis Pshenov

Reputation: 11327

I found the 'answer'.

Ruby unlike Node.js does not have a built in support for local node_modules like implementation. Thus you have to use Bundler. But to use the dependencies in your code you need to let Ruby know where these dependencies are located. Bundler makes it easy for you with the following lines in your app code:

require 'rubygems'
require 'bundler/setup'

This will read your Gemfile.lock and apply proper paths when your require something form your app. This is the part that I missed.

You can also use bundle exec {rubygem} to run gems that are installed by bundler for your app and not those that are installed globally.

I hope this helps someone!

Upvotes: 13

Related Questions