Getting Started
Introduction
8 • Modules

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

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

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

    File Require Statements

    Let's decipher these files and what they are doing:

    • The main script requires sibling and modules next to it
    • The modules/init script requires module next to it, and sibling going up one directory using ../

    In the above require statements, we can see that are relative to the file that they are in, and in Lune this is always the case, except for built-in libraries, which always start with an at sign (@).

    Q: Wait, hold on... The main script requires the directory called modules?
    A: Yep, that's right. The file name init is special, and putting a file named init.luau in a directory will let you use require directly on the directory. Similar to index.js in JavaScript or mod.rs in Rust.