Modules
At this point you know how the most important built-in libraries in Lune work and how to use them, and your code is probably getting longer and more difficult to read. Splitting your code into multiple files can help you stay organized.
Modularizing your code and splitting it across several files in Lune is different from other versions of Lua and Luau, and more similar to how things work in other languages such as JavaScript.
Example File Tree
Section titled “Example File Tree”Let’s use this directory & file tree structure for our examples:
- main.luau
- sibling.luau
Directorymodules
- init.luau
- module.luau
local sibling = require("./sibling")local modules = require("./modules")
print(sibling.Hello) --> World
print(modules.Child.Foo) --> Barprint(modules.Child.Fizz) --> Buzz
print(modules.Sibling.Hello) --> World
return { Hello = "World",}
return { Child = require("@self/child"), Sibling = require("./sibling"),}
return { Foo = "Bar", Fizz = "Buzz",}
File Require Statements
Section titled “File Require Statements”Let’s decipher these modules and what they are doing:
- The
main
module requiressibling
andmodules
next to it - The
modules
module requireschild
inside of it using the special@self
alias - The
modules
module requiressibling
next to it using./
This is the standard behavior according to Luau require-by-string, which you can read more about on the official Luau documentation site.