> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# loadfile

> Reads a workspace file, compiles its contents with the Luau compiler, and returns the loaded Luau function without executing it.

Reads a workspace file, compiles its contents with the Luau compiler, and returns the loaded Luau function without executing it.

## Syntax

```lua theme={null}
loadfile(path: string, chunkName?: string): function | nil | string | nil
```

## Aliases

* `loadfileasync`
* `dofile`
* `dofileasync`

## Arguments

| Name        | Type     | Required | Description                                                                                                                                                          |
| ----------- | -------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `path`      | `string` | Yes      | File path resolved inside the workspace after separator normalization, workspace-boundary checks, and extension checks. Path, open, and read-size errors are thrown. |
| `chunkName` | `string` | No       | Optional chunk name passed to luau\_load. Defaults to = when omitted or nil.                                                                                         |

## Returns

| Name           | Type              | Description                                                                                                          |
| -------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------- |
| `chunk`        | `function \| nil` | Loaded Luau function when luau\_load succeeds. The function is not executed by loadfile.                             |
| `errorMessage` | `string \| nil`   | Error string returned as the second value only when luau\_load fails. Filesystem and path errors are thrown instead. |

## Example

Load a workspace Luau file as a function, then call it yourself.

```lua theme={null}
writefile("examples/module.luau", "return 2 + 3")

local chunk, message = loadfile("examples/module.luau", "ExampleModule")

if chunk then
    print(chunk())
else
    warn(message)
end
```
