Skip to content

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.

Let’s use this directory & file tree structure for our examples:

  • main.luau
  • sibling.luau
  • Directorymodules
    • init.luau
    • module.luau
main.luau
local sibling = require("./sibling")
local modules = require("./modules")
print(sibling.Hello) --> World
print(modules.Child.Foo) --> Bar
print(modules.Child.Fizz) --> Buzz
print(modules.Sibling.Hello) --> World

Let’s decipher these modules and what they are doing:

  • The main module requires sibling and modules next to it
  • The modules module requires child inside of it using the special @self alias
  • The modules module requires sibling 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.