# Require (/docs/es_extended/client/imports/require)

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 [#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 |

```lua title="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',
}
```

<Callout type="warn">
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.
</Callout>

## Module names [#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 [#functions]

### require / ESX.require [#require--esxrequire]

```lua
require(modName)
```

| Name | Type | Description |
|---|---|---|
| `modName` | `string` | Module name as described above. |

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

```lua title="modules/garage/config.lua"
return {
    spots = {
        vector4(229.7, -800.1, 30.6, 160.0),
        vector4(233.3, -801.4, 30.6, 160.0),
    },
}
```

```lua title="client/main.lua"
local garage = require 'modules.garage.config'

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

### ESX.load [#esxload]

```lua
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 [#esxloadjson]

```lua
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.

```lua title="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 [#other-client-imports]

`@es_extended/imports.lua` also loads `client/imports/class.lua` on the client and exposes it as `ESX.Class`.

```lua
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.

```lua title="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())
```

## Related [#related]

- [Points](/docs/es_extended/client/imports/point)
- [esx_lib](/docs/core/esx_lib)
- [Your first resource](/docs/getting-started/first-resource)
- [Coding practices](/docs/getting-started/coding-practices)
