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?
To determine its path, require first checks the global variable LUA_PATH . If the value of LUA_PATH is a string, that string is the path. Otherwise, require checks the environment variable LUA_PATH .
Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy. A single dot represents the current directory itself.
The dofile function is actually an auxiliary function; loadfile does the hard work. Like dofile , loadfile also loads a Lua chunk from a file, but it does not run the chunk. Instead, it only compiles the chunk and returns the compiled chunk as a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With