53
votes

My directory structure looks like this:

|-- ball.lua
|-- entity.lua
|-- test
    `-- ball_test.lua

I'm using the following code in test/ball_test.lua to require ball.lua from the parent directory:

package.path = package.path .. ";../entity.lua"
require("entity")
package.path = package.path .. ";../ball.lua"
require("ball")

entity.lua is a dependency of ball.lua. So I require("entity") first otherwise I get a module 'entity.lua' not found error. This seems like a hack, what's a better way to do this?

1
have ball module require entity.lua, so you just have to require'ball' - Ɓukasz Gruner
@Lukasz - ball does require entity.lua. I get the error because require can't find entity.lua when it's executed from the test subdirectory. Sorry I didn't make that clear in my question. - Seth Reno

1 Answers

81
votes
package.path = package.path .. ";../?.lua"

will work for both.