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

# Server loader

> The Bun process that runs the server build of your game

## What it does

The server loader (`@nanoforge-dev/loader-server`) runs the server app of your game.
[`nf start`](https://docs.nanoforge.eu/docs/cli/commands/start) starts one for each server app, pointed at the app's build
folder (`-d, --dir <dir>`, required).

It lists the files of the build folder, finds `main.js` at its root, and starts a child process
(the worker) that loads `main.js` and calls its `main` function. When the worker exits (a crash,
or a `process.exit()` in your game), the server loader stops too, even with `--watch`.

## What your server's main receives

| Field | Type | Description |
| - | - | - |
| `files` | `Map<string, string>` | Each file of the build (`/map.json`) mapped to its full path on disk. |
| `env` | `Record<string, string \| undefined>` | The server's environment variables, without the `NANOFORGE_` prefix. |
| `editor` | object, optional | Set only when the NanoForge editor runs the game: the channel between game and editor. |

The `main.ts` that `nf new` generates passes this object to `app.init`.

`files` is built once, when the loader starts. A file added to the build later is not in it until
the loader restarts (stop and start `nf dev` or `nf start`).

## Restart on change

With `--watch` (set by [`nf dev`](https://docs.nanoforge.eu/docs/cli/commands/dev)), the loader watches the build folder.
When a file changes, it stops the worker and starts a new one, with a clean state. Changes closer
than 100 ms count as one restart.

Players connected to the old worker lose their connection when it restarts.

## Common errors

* **`No game dir specified`**: the loader was started without `-d`. `nf start` always passes it;
  check the server's `out.dir`.
* **`No main.js found`**: the build folder has no `main.js` at its root. Run `nf build`, and keep
  the server entry file named `main.ts`.
* **An error thrown by your game**: the worker prints it and exits, and the server loader stops
  with it. Fix the error, then restart `nf dev` (or `nf start`).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.