---
title: "Node.js"
description: "Run libfx with a native addon or WebAssembly in Node.js."
canonical_url: https://fx.sh/docs/lib/node
markdown_url: https://fx.sh/docs/lib/node.md
---

# Node.js

The Node.js entry point runs the agent through a native addon, with WebAssembly as a fallback.

## Install

Use Node.js 20.19 or later in the 20.x line, or Node.js 22.12 or later (`^20.19.0 || >=22.12.0`):

```sh
npm install libfx@0.0.16
```

Bun runs libfx as Node.js does. For the WebAssembly backend, use Bun 1.4.2 or later.

Follow [Quick start](https://fx.sh/docs/lib/quick-start.md) to connect a model and run your first prompt.

## Choose an entry point

| Import | Description | Environment |
| --- | --- | --- |
| `libfx` | The entry point for the current environment | Node or browser |
| `libfx/node` | Native addon first, WebAssembly fallback | Node |
| `libfx/browser` | WebAssembly | Browser |
| `libfx/wasm` | The engine and terminal on WebAssembly, without packaged asset paths | Node or browser |
| `libfx/durable-local`, `libfx/durable-vercel`, `libfx/durable-world` | [Where sessions live](https://fx.sh/docs/lib/durability.md#where-sessions-live) | Node |
| `libfx/chatgpt`, `libfx/grok` | [Sign-in for ChatGPT and Grok plans](https://fx.sh/docs/lib/providers.md) | Node |
| `libfx/mcp`, `libfx/skills`, `libfx/skills/node` | [MCP](https://fx.sh/docs/lib/mcp.md) and [skills](https://fx.sh/docs/lib/skills.md) adapters | Node or browser; `skills/node` in Node or Bun |

Node applications can also use `require('libfx')`. With TypeScript's `moduleResolution: 'bundler'`, add `'node'` to `customConditions` to get the Node.js types.

## Choose a backend

`backend: 'auto'` is the default. It tries the native addon first and loads the WebAssembly backend only when native execution is unavailable. Set `backend: 'native'` when you want the agent to fail if the addon is unavailable, or `backend: 'wasm'` to use WebAssembly only.

libfx has native addons for Linux and macOS on x64 and arm64, one package each. libfx lists them as optional dependencies, so your package manager installs only the package for your platform:

| Platform | Package |
| --- | --- |
| macOS on arm64 | `@libfx/libfx-darwin-arm64` |
| macOS on x64 | `@libfx/libfx-darwin-x64` |
| Linux on arm64 | `@libfx/libfx-linux-arm64-gnu` |
| Linux on x64 | `@libfx/libfx-linux-x64-gnu` |

Keep optional dependencies enabled when installing or deploying with the native backend. Linux addons need glibc 2.34 or later. Windows uses the WebAssembly fallback. The native backend doesn't need JSPI or experimental Node flags; every WebAssembly path needs [JSPI](https://fx.sh/docs/lib/webassembly.md#check-runtime-support). On the native backend, calls from one model response can run at the same time, as [Run calls together](https://fx.sh/docs/lib/tools.md#run-calls-together) describes.

The packaged native addon implements the agent's engine, so `createFxTerminal()` uses WebAssembly. See [the WebAssembly terminal](https://fx.sh/docs/lib/webassembly.md#embed-the-terminal) for that API and [AgentOptions](https://fx.sh/docs/lib/api.md#agentoptions) for supplying your own addon or WebAssembly asset.

<span id="nextjs-and-vercel" />

## Use the agent in a server route

Create the agent once at module scope, and use it from every route:

```js
// lib/agent.js
import { createFxAgent } from 'libfx'

export const agent = createFxAgent({ model: 'anthropic/claude-opus-5.5', tools })
```

Then return a session's stream from a route, as [Prompts and streaming](https://fx.sh/docs/lib/prompts.md#stream-to-a-browser) shows. Don't create or close an agent per request: the agent keeps running a turn after the response ends. On Vercel, add the queue route from [Durability](https://fx.sh/docs/lib/durability.md#on-vercel).

Native agents support Next.js 15 with webpack and Next.js 16 with webpack or Turbopack, including standalone builds. The matching optional platform package supplies the native asset, so you don't need `serverExternalPackages` or manual file tracing. Include that package in the deployment, and build on the platform you deploy to: a standalone build made on macOS carries only the macOS addon, so it can't use the native backend on Linux. If you use WebAssembly in a standalone build, supply its assets yourself, because the standalone tracer leaves out `.wasm` files.

## Handle a missing backend

```js
import { getBackendInfo } from 'libfx'

const info = await getBackendInfo({ surface: 'agent', backend: 'auto' })
console.log(info.backend)
console.log(info.attempts)
```

`getBackendInfo()` checks whether the native addon or WebAssembly can load. It doesn't validate credentials or make a model request, though a remote WebAssembly asset may still need a fetch. Use `surface: 'terminal'` to check terminal support.

When the package for your platform isn't installed, for example because optional dependencies were omitted or the lockfile came from another platform, `backend: 'auto'` uses WebAssembly instead. `getBackendInfo()` reports the native attempt as `LIBFX_NATIVE_ARTIFACT_MISSING`, with a message that names the package, such as `the native addon package @libfx/libfx-linux-x64-gnu for linux-x64 is not installed`. Install that package, or reinstall without `--omit=optional`, to use the native backend.

`LIBFX_JSPI_REQUIRED` means the WebAssembly path needs JSPI. If the platform package is missing, the error names it. On Node versions that need a flag, start with `node --experimental-wasm-jspi agent.mjs`. With `backend: 'native'`, `LIBFX_NATIVE_UNAVAILABLE` means the native addon can't provide the requested agent or terminal. The attempts from [`getBackendInfo()`](https://fx.sh/docs/lib/api.md#getbackendinfo) tell missing files, unsupported platforms, and other loading failures apart.

## Migrating from 0.0.15

- Update Node.js to `^20.19.0 || >=22.12.0`. This requirement also applies to `require('libfx')`.
- The native addons moved out of `libfx` into one optional package per platform, so an install is now less than half the size of 0.0.15's. Keep optional dependencies enabled so the addon for your platform is installed. Without it, the default backend falls back to WebAssembly, which requires JSPI. A standalone build carries only the addon for the platform it was built on. See [Choose a backend](#choose-a-backend).
- Upgrade every worker sharing a session store before starting new turns. Saved turns with context updates use journal and snapshot version 4, which libfx 0.0.15 cannot read. libfx 0.0.16 reads earlier formats. See [Save and restore](https://fx.sh/docs/lib/checkpoints.md#compatibility).

## Migrating from 0.0.14

- Engines using `provider: 'codex'` or `'grok'` require an explicit model from that provider. Durable agents can set it on the agent or each session. See [Providers](https://fx.sh/docs/lib/providers.md).
- `prompt()` rejects empty prompts and invalid image input before a durable session queues or saves it. With `resizeImage`, image content and size checks apply to its output when the turn runs. See [Images](https://fx.sh/docs/lib/images.md#supported-images).
- Earlier libfx versions can't open journals or snapshots that use the new saved-image references. libfx 0.0.15 still opens sessions from earlier versions.

## Migrating from 0.0.13

In libfx 0.0.13 and earlier, `createFxAgent()` created one in-memory conversation. That's now `createFxEngine()`, and `createFxAgent()` creates the durable agent:

- To keep the old behavior, rename `await createFxAgent(options)` to `await createFxEngine(options)`.
- To move to sessions, call `createFxAgent(options)` without `await`, then `agent.session().prompt()`.

Code written for 0.0.13 runs against the agent once its tools set `idempotent`, with a few differences: breaking out of a turn's events no longer cancels it, turns have no `cancel()` or `steer()` (the session does), and `persistence` and `sessionId` throw because the durability holds the session.

Three changes can break existing code:

- Every tool given to `createFxAgent()` must set `idempotent` to `true` or `false`. Without it, `createFxAgent()` throws an error that names the tool. See [Tools with effects](https://fx.sh/docs/lib/durability.md#tools-with-effects).
- A tool's `writes` now defaults to the opposite of `idempotent`, in `createFxEngine()` too, so a tool that isn't marked idempotent runs alone.
- A journal written by 0.0.14 can't be loaded by an older libfx, which fails with `FxJournalVersionError`.

## Migrating from 0.0.7

The agent API changed in 0.0.8, and again in 0.0.14. These steps lead to the 0.0.13 API; then follow [Migrating from 0.0.13](#migrating-from-0013):

- Pass `apiKey` and `model` directly instead of through `env`.
- Call `agent.prompt()` instead of `agent.createSession()` followed by `session.prompt()`.
- Save conversation checkpoints, and supply tools and instructions again when you restore.

For the old API, use the [0.0.7 SDK README](https://github.com/vercel-labs/fx/blob/cef08aa0f178537e552a931c7863dc4c1487e4a0/sdk/README.md).

<span id="run-a-headless-agent" />
<span id="save-and-restore-a-conversation" />
<span id="what-the-embedded-agent-can-do" />
<span id="add-tools" />
<span id="connect-mcp-and-skills" />
<span id="discover-models" />
<span id="security-boundaries" />

## Agent guides

- [Prompts and streaming](https://fx.sh/docs/lib/prompts.md)
- [Durability](https://fx.sh/docs/lib/durability.md)
- [Tools](https://fx.sh/docs/lib/tools.md)
- [Models](https://fx.sh/docs/lib/models.md) and [Providers](https://fx.sh/docs/lib/providers.md)
- [Save and restore](https://fx.sh/docs/lib/checkpoints.md)
- [MCP](https://fx.sh/docs/lib/mcp.md) and [Skills](https://fx.sh/docs/lib/skills.md)

---

[Browse all fx documentation](https://fx.sh/llms.txt)
