Require
Load Lua modules in ESX Legacy with require, ESX.require, ESX.load and ESX.loadJson from es_extended and esx_lib imports, including modules from other resources.
FiveM does not ship a working Lua require. ESX provides one so you can split a resource into modules and load them on demand, including from other resources. You get it automatically when your manifest loads @es_extended/imports.lua and/or @esx_lib/imports.lua.
Overview
| Provided by | @es_extended/imports.lua (as ESX.require, only when ox_lib is missing) and @esx_lib/imports.lua (as xLib.require) |
| Side | Client and server |
| Global | require is replaced by one of the above |
fx_version 'cerulean'
game 'gta5'
lua54 'yes'
shared_scripts {
'@esx_lib/imports.lua',
'@es_extended/imports.lua',
}
client_scripts { 'client/main.lua' }
server_scripts { 'server/main.lua' }
files {
'modules/**/*.lua',
'data/*.json',
}The module loader in @es_extended/imports.lua uses xLib internally. Load @esx_lib/imports.lua before it, as the official ESX resources do, or loading a module file fails.
Module names
- A module name is a path relative to the resource root, without
.lua. - Use
.(or/) to separate folders:modules.inventory.utilsloadsmodules/inventory/utils.lua. - If no file matches,
modules/inventory/utils/init.luais tried. @resource.pathloads from another resource, for example@my_utils.shared.math.- Results are cached per resource. The second
requireof the same name returns the cached value. - Two modules that require each other raise a "circular-dependency" error.
Functions
require / ESX.require
require(modName)| Name | Type | Description |
|---|---|---|
modName | string | Module name as described above. |
Returns: whatever the module file returns, or true if it returns nothing.
return {
spots = {
vector4(229.7, -800.1, 30.6, 160.0),
vector4(233.3, -801.4, 30.6, 160.0),
},
}local garage = require 'modules.garage.config'
for i = 1, #garage.spots do
print(garage.spots[i])
endESX.load
ESX.load(filePath, env)Loads and runs a Lua file like require, but does not cache it. Each call runs the file again.
| Name | Type | Description |
|---|---|---|
filePath | string | Module name. |
env | table? | Environment for the chunk. Default: the calling resource's environment. |
Returns: what the file returns. Raises an error when the file is not found.
ESX.loadJson
ESX.loadJson(filePath)Reads <filePath>.json and decodes it.
| Name | Type | Description |
|---|---|---|
filePath | string | Path without .json. Dots become folder separators, so use data.items for data/items.json. |
Returns: table with the decoded JSON. Raises an error when the file is missing.
local prices = ESX.loadJson('data.prices')
print(prices.bread)ESX.load and ESX.loadJson exist only when ox_lib is not running. With @esx_lib/imports.lua you can always use xLib.load and xLib.loadJson, which behave the same.
Other client imports
@es_extended/imports.lua also loads client/imports/class.lua on the client and exposes it as ESX.Class.
ESX.Class(body, heritage)| Name | Type | Description |
|---|---|---|
body | table? | Prototype with methods. A constructor method runs on :new(...). |
heritage | table? | Parent class to inherit from. |
Returns: the class. Create instances with MyClass:new(...). If constructor returns a table, that table is returned instead of the instance.
local Vehicle = ESX.Class({
constructor = function(self, plate)
self.plate = plate
end,
describe = function(self)
return ('Vehicle %s'):format(self.plate)
end,
})
local car = Vehicle:new('ESX 123')
print(car:describe())Related
Frequently asked questions
Independent documentation
Last updated on
Points
ESX.Point creates proximity points with enter, leave and inside callbacks in ESX Legacy, replacing distance loops. Learn the options, methods and pitfalls.
Shared Functions
ESX Legacy functions available on both client and server - GetConfig, weapon lookups, ValidateType, IsValidLocaleString, GetRandomString and ESX.Await.