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.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading