# Help with relative paths using require

**URL:** <https://www.ruby-forum.com/t/help-with-relative-paths-using-require/172320>\
**Category:** Ruby\
**Created:** [July 19, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/help-with-relative-paths-using-require/172320 "2009-07-19T16:14:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![typemismatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@typemismatch](https://www.ruby-forum.com/u/typemismatch)\
**Post date:** [July 19, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/help-with-relative-paths-using-require/172320/1 "2009-07-19T16:14:04Z")

</div>

Hi,

I’m having a lot of problems trying to “require” my own .rb files from  
a lib folder in my project.

My structure is simple enough.

app  
app\lib  
app\feature  
app\feature2

I have all shared code in \lib and in any feature code I do a require  
‘…\lib\whatever.rb’

It runs fine locally but if I run on a server using Cron or a Daemon  
the base path seems to be always set to something else so …\lib  
doesn’t exist. My server and my dev. environments are very different  
so it means hacking server deployments to find library files.

What is the correct method here? To wrap my lib code in a gem?

thanks!  
-c

---

<div class="post-metadata">

**Author:** ![typemismatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@typemismatch](https://www.ruby-forum.com/u/typemismatch)\
**Post date:** [July 19, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/help-with-relative-paths-using-require/172320/2 "2009-07-19T17:03:44Z")

</div>

Hi –

On Sun, 19 Jul 2009, typemismatch wrote:

> app\feature2
> 
> I have all shared code in \lib and in any feature code I do a require  
> ‘…\lib\whatever.rb’
> 
> It runs fine locally but if I run on a server using Cron or a Daemon  
> the base path seems to be always set to something else so …\lib  
> doesn’t exist. My server and my dev. environments are very different  
> so it means hacking server deployments to find library files.
> 
> What is the correct method here? To wrap my lib code in a gem?

Does Tony A.'s require\_rel (part of require\_all) help?  
[http://github.com/tarcieri/require\_all/tree/master](http://github.com/tarcieri/require_all/tree/master)

David

---

<div class="post-metadata">

**Author:** ![typemismatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@typemismatch](https://www.ruby-forum.com/u/typemismatch)\
**Post date:** [July 19, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/help-with-relative-paths-using-require/172320/3 "2009-07-19T22:08:49Z")

</div>

Thanks David, the require\_all was perfect! and easier than me  
packaging gems 🙂

-c

---

<div class="post-metadata">

**Author:** ![typemismatch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@typemismatch](https://www.ruby-forum.com/u/typemismatch)\
**Post date:** [July 19, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/help-with-relative-paths-using-require/172320/4 "2009-07-19T18:57:06Z")

</div>

On Sunday 19 July 2009 09:13:37 am typemismatch wrote:

> My structure is simple enough.
> 
> app  
> app\lib  
> app\feature  
> app\feature2
> 
> I have all shared code in \lib and in any feature code I do a require  
> ‘…\lib\whatever.rb’

Quick question: Where are you actually running the app from? I’m  
guessing that  
path isn’t relative to the file the ‘require’ statement is in, but  
rather, from  
the current working directory.

> It runs fine locally but if I run on a server using Cron or a Daemon  
> the base path seems to be always set to something else so …\lib  
> doesn’t exist.

Among other things, I’m guessing you’ll find the working directory is  
somewhere  
else. But you could play with $: a bit.

> What is the correct method here? To wrap my lib code in a gem?

That’s a good idea anyway, but you could also do something like this:

require ‘pathname’  
$: \<\< Pathname( **FILE** ).parent.join(‘lib’).to\_s

Now, you could just do:

require ‘whatever’

The .rb is implied, and the path includes ‘lib’ directly, now. You’d get  
something similar if you packaged it as a gem.
