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

# @nanoforge-dev/ecs

[![NanoForge](https://raw.githubusercontent.com/NanoForge-dev/Engine/main/.github/logo.png)](https://github.com/NanoForge-dev)

[![npm version](https://img.shields.io/npm/v/@nanoforge-dev/ecs.svg?maxAge=3600)](https://www.npmjs.com/package/@nanoforge-dev/ecs)

[![npm downloads](https://img.shields.io/npm/dt/@nanoforge-dev/ecs.svg?maxAge=3600)](https://www.npmjs.com/package/@nanoforge-dev/ecs)

[![Tests status](https://github.com/NanoForge-dev/Engine/actions/workflows/tests.yml/badge.svg)](https://github.com/NanoForge-dev/Engine/actions/workflows/tests.yml)

[![Last commit](https://img.shields.io/github/last-commit/NanoForge-dev/Engine.svg?logo=github\&logoColor=ffffff\&path=modules%2Fecs)](https://github.com/NanoForge-dev/Engine/commits/main/modules/ecs)

[![Contributors](https://img.shields.io/github/contributors/NanoForge-dev/Engine.svg?maxAge=3600\&logo=github\&logoColor=fff\&color=00c7be)](https://github.com/NanoForge-dev/Engine/graphs/contributors)

## About

`@nanoforge-dev/ecs` is NanoForge's built-in entity-component-system library, backed by a WebAssembly registry (compiled from C++). It ships two subpath entry points — `@nanoforge-dev/ecs/client` and `@nanoforge-dev/ecs/server` — each loading its own WASM build (browser-targeted vs Node-targeted) behind the same `EcsLibrary` API, so a client bundle never pulls in the server build or vice versa. The root entry point, `@nanoforge-dev/ecs`, only exposes the shared types (`Registry`, `Component`, `System`, `Entity`, `SparseArray`, `Context`, `EcsContextApi`) and pulls in no WASM binary, so it's safe to import from code that targets both client and server.

## Installation

**Node.js 26 or newer is required.**

```sh theme={null}
npm install @nanoforge-dev/ecs
yarn add @nanoforge-dev/ecs
pnpm add @nanoforge-dev/ecs
bun add @nanoforge-dev/ecs
```

## Example usage

Register `EcsLibrary` from the entry point matching your app:

```ts theme={null}
// Client app
import { EcsLibrary } from "@nanoforge-dev/ecs/client";

app.use(new EcsLibrary());
```

```ts theme={null}
// Server app
import { EcsLibrary } from "@nanoforge-dev/ecs/server";

app.use(new EcsLibrary());
```

Shared types like `Registry`, `Component`, and `System` are available from the root entry point, regardless of which platform you're targeting:

```ts theme={null}
import type { Component, Context, Registry, System } from "@nanoforge-dev/ecs";

const Position: Component = { name: "Position" };

const gravity: System = (registry: Registry, ctx: Context) => {
  // ...
};
```

Once registered, `Context.ecs.registry` gives every other library access to the entity/component registry:

```ts theme={null}
public override async __run(ctx: Context): Promise<void> {
  const entity = ctx.ecs.registry.spawnEntity();
  ctx.ecs.registry.addComponent(entity, new Position(0, 0));
}
```

Register systems to run on every tick:

```ts theme={null}
ctx.ecs.registry.addSystem((registry) => {
  for (const { __RESERVED_entityId, Position } of registry.getZipper([
    { name: "__RESERVED_entityId" },
    { name: "Position" },
  ])) {
    // Move things around.
  }
});
```

When registered alongside `@nanoforge-dev/editor`'s `EditorLibrary`, `EcsLibrary` also listens for a `"hot-reload"` command from the editor and applies incoming components via `registry.addComponent` — useful for live-editing entity state from an external editor host.

### Scenes

`@nanoforge-dev/ecs/scene` has `EcsScene`, a scene of `@nanoforge-dev/scene` whose entities and systems go away when it is unloaded. Write its `setup` like an app's `main`:

```ts theme={null}
import { EcsScene } from "@nanoforge-dev/ecs/scene";

export class Level1 extends EcsScene {
  static override parent = Run;

  override setup(registry: Registry, ctx: Context) {
    const wall = registry.spawnEntity();
    registry.addComponent(wall, new Position(40, 90));
    registry.addSystem(levelFlow);
  }
}
```

The registry `setup` gets is the app's, tracked: at unload, the systems added through it are removed (by function) and the entities spawned through it are killed, after calling the `destroy()` of their components that have one. `teardown(registry, ctx)` runs first, if the scene has one.

## Links

* [GitHub][source]
* [npm][npm]

## Contributing

Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
[documentation][documentation].\
See [the contribution guide][contributing] if you'd like to submit a PR.

## Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to ask questions in [discussions][discussions].

[documentation]: https://github.com/NanoForge-dev/Engine

[discussions]: https://github.com/NanoForge-dev/Engine/discussions

[source]: https://github.com/NanoForge-dev/Engine/tree/main/modules/ecs

[npm]: https://www.npmjs.com/package/@nanoforge-dev/ecs

[contributing]: https://github.com/NanoForge-dev/Engine/blob/main/.github/CONTRIBUTING.md

## Modules

* [client](https://docs.nanoforge.eu/references/engine/3.0.0/ecs/client/index)
* [index](https://docs.nanoforge.eu/references/engine/3.0.0/ecs/index/index)
* [scene](https://docs.nanoforge.eu/references/engine/3.0.0/ecs/scene/index)
* [server](https://docs.nanoforge.eu/references/engine/3.0.0/ecs/server/index)


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