Is there a better way to require file from relative path in lua

Lua

Lua Problem Overview


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?

Lua Solutions


Solution 1 - Lua

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

will work for both.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeth RenoView Question on Stackoverflow
Solution 1 - LuaDoug CurrieView Answer on Stackoverflow