Skip to content

loadfile

loadfile compiles the Lua source code from a file and returns the resulting function (chunk). This chunk runs in the global environment.

If the file contains syntax errors, an error is thrown - just like loadstring. The error will appear in the console.

function loadfile<A...>(path: string): ((A...) -> any | nil, string?)

Parameters

Parameter Description
path The path to the file to be loaded.

Examples

Example 1

Loading and executing a valid file
1
2
3
writefile("file6.lua", "return 10 + ...")
local chunk = loadfile("file6.lua")
print(chunk(1)) -- Output: 11

Example 2

Triggering a syntax error
writefile("file6.lua", "retrn 10 + ...")
loadfile("file6.lua") -- This will throw an error in the console