Search code examples
rubybundlerrequire

How to add 'lib' to LOAD_PATH using Gemfile without gemspec?


I started a small project with bundle init. Actually I build an app, not a gem, so I decided not to use .gemspec file. And I faced with the problem that I can not use require to require files located in lib. For example, on bundle exec ruby bin/console I get:

cannot load such file -- new/gem

With gemspec file it will work because it has require_paths = ["lib"] option by default and after require "bundler/setup" you can use require to access files located in lib.

If I do not use gemspec file, how can I hint Bundler that I would like to have possibility to require from lib?


Solution

  • There's a few ways:

    1. Use require_relative "../lib/app"
    2. Add lib to $LOAD_PATH in bin/console: $LOAD_PATH.unshift "./lib"
    3. Just add it inline on the command line: bundle exec ruby -Ilib bin/console

    Note though that if you're using require_relative in your app (and you should be), then lib doesn't need to be in $LOAD_PATH, you should just be able to require "path/to/app" wherever you want to load it.