FiveM Docs
es_extendedClientImports

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)
SideClient and server
Globalrequire is replaced by one of the above
fxmanifest.lua
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.utils loads modules/inventory/utils.lua.
  • If no file matches, modules/inventory/utils/init.lua is tried.
  • @resource.path loads from another resource, for example @my_utils.shared.math.
  • Results are cached per resource. The second require of the same name returns the cached value.
  • Two modules that require each other raise a "circular-dependency" error.

Functions

require / ESX.require

require(modName)
NameTypeDescription
modNamestringModule name as described above.

Returns: whatever the module file returns, or true if it returns nothing.

modules/garage/config.lua
return {
    spots = {
        vector4(229.7, -800.1, 30.6, 160.0),
        vector4(233.3, -801.4, 30.6, 160.0),
    },
}
client/main.lua
local garage = require 'modules.garage.config'

for i = 1, #garage.spots do
    print(garage.spots[i])
end

ESX.load

ESX.load(filePath, env)

Loads and runs a Lua file like require, but does not cache it. Each call runs the file again.

NameTypeDescription
filePathstringModule name.
envtable?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.

NameTypeDescription
filePathstringPath 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.

server/main.lua
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)
NameTypeDescription
bodytable?Prototype with methods. A constructor method runs on :new(...).
heritagetable?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.

client/main.lua
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())

Frequently asked questions

Independent documentation

esx-fivem.com is a community project and is not affiliated with the ESX Framework team. Facts on this page were checked against the upstream source. Official resources: docs.esx-framework.org and github.com/esx-framework.
Edit on GitHub

Last updated on

On this page