Codectionary / Developer documentation / Lua

Lua Modules

Lua modules typically return a table of public functions and values. require loads a module and returns the value it exported, caching it for later requests.

Syntax

-- math_helpers.lua
local M = {}
return M

local helpers = require("math_helpers")

Examples

Exporting a module function

A small, runnable example of this syntax.

-- math_helpers.lua
local M = {}
function M.double(value)
  return value * 2
end
return M

-- main.lua
local helpers = require("math_helpers")
print(helpers.double(4))

Best practices

  • Return one clear module table and avoid mutable module-level state unless it is deliberately part of the module’s contract.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Lightweight scripting and embedding
File extension
.lua
Runs in
Lua interpreter or a host application
Usually used with
Host APIs and Lua modules

Specifications & further reading

Related Lua documentation