> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch02-getting-started/2.1-environment-setup.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 2.1 Environment Setup

> **Generated by**: Claude Opus 4.6 (initial draft) / Claude Opus 5 (`v2026.3.9` revision) **Token usage**: Input \~140,000 tokens, Output \~10,000 tokens (total for this chapter) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

Before diving into the OpenClaw source code, you need to set up a complete development environment. This section will guide you through all the preparation steps from zero to a working setup.

## 2.1.1 Node.js 22+ and pnpm Installation

OpenClaw has explicit version requirements for the runtime. The `package.json` declares:

```json
{
  "engines": {
    "node": ">=22.12.0"
  },
  "packageManager": "pnpm@10.23.0"
}
```

### Installing Node.js

It's recommended to use a version manager (such as [nvm](https://github.com/nvm-sh/nvm), [fnm](https://github.com/Schniz/fnm), or [volta](https://volta.sh/)) to install Node.js, which makes it convenient to switch versions between different projects.

```bash
# Install Node.js 22 using nvm
nvm install 22
nvm use 22

# Verify the version
node --version   # Should output v22.x.x
```

If your system already has Node.js installed but the version is below 22, OpenClaw will throw an error on startup. This check is implemented in `src/infra/runtime-guard.ts` — the `assertSupportedRuntime()` function compares the current Node.js version against the `engines.node` requirement in `package.json`.

### Installing pnpm

OpenClaw uses pnpm 10.x as its package manager. Node.js 16+ comes with Corepack built-in, so you can enable pnpm directly:

```bash
# Enable Corepack (Node.js's built-in package manager management tool)
corepack enable

# Verify pnpm version
pnpm --version   # Should output 10.x.x
```

> **Sidebar**: Corepack is an experimental tool built into Node.js that manages the versions of package managers (npm, yarn, pnpm). It reads the `packageManager` field in the project's `package.json` and automatically downloads and uses the corresponding version of pnpm. This ensures that everyone on the team uses exactly the same version of pnpm.

If Corepack is unavailable, you can also install manually:

```bash
npm install -g pnpm@10
```

## 2.1.2 Cloning and Building from Source

```bash
# Clone the repository
git clone https://github.com/openclaw/openclaw.git
cd openclaw

# Install dependencies (pnpm automatically handles dependencies for all packages in the Monorepo)
pnpm install
```

After `pnpm install` completes, you'll notice a `node_modules/` directory appears in the root directory as well as in each sub-package. Due to pnpm's hard-linking mechanism, the actual package files are stored in a global `.pnpm-store`, and the project directory only contains symbolic links, so disk usage is much smaller than with npm.

Next, build the project:

```bash
# Build core code (tsdown compiles TypeScript → JavaScript)
pnpm build
```

The `pnpm build` command actually executes a series of steps defined in `package.json`'s `scripts.build`:

```json
{
  "scripts": {
    "build": "pnpm canvas:a2ui:bundle && tsdown && pnpm build:plugin-sdk:dts && node --import tsx scripts/write-plugin-sdk-entry-dts.ts && node --import tsx scripts/canvas-a2ui-copy.ts && node --import tsx scripts/copy-hook-metadata.ts && node --import tsx scripts/write-build-info.ts && node --import tsx scripts/write-cli-compat.ts"
  }
}
```

Breaking down this build pipeline:

1. **`pnpm canvas:a2ui:bundle`**: Bundles the Canvas A2UI frontend code
2. **`tsdown`**: Core TypeScript compilation (using `tsdown.config.ts` configuration)
3. **`build:plugin-sdk:dts`**: Generates type declaration files for the plugin SDK
4. **Subsequent scripts**: Copy metadata, write build information, generate CLI compatibility layer

Build artifacts are output to the `dist/` directory:

```
dist/
├── index.js               # Main entry point (library mode)
├── entry.js               # CLI entry point
├── extensionAPI.js         # Extension API
├── plugin-sdk/             # Plugin SDK
│   ├── index.js
│   └── index.d.ts         # Type declarations
└── ...
```

## 2.1.3 UI Build

The Web control console UI is a separate package and needs to be built independently:

```bash
# Build the Web UI (first run will automatically install UI dependencies)
pnpm ui:build
```

This command is executed through the `scripts/ui.js` script, which will:

1. Check if UI dependencies are installed; if not, automatically run `pnpm install`
2. Use Vite to build Lit Web Components
3. Output static assets to the UI's `dist/` directory

After building, the UI files will be loaded by the Gateway on startup and served via HTTP (`src/gateway/control-ui.ts`). Users can access the control panel through a browser at `http://127.0.0.1:18789`.

Complete one-command build:

```bash
# Install dependencies → Build UI → Build core code
pnpm install && pnpm ui:build && pnpm build
```

After a successful build, you can verify it as follows:

```bash
# Check if dist/ was generated
ls dist/index.js

# Try running the CLI help
node dist/entry.js --help
```

If you see the `openclaw` command-line help information, the build was successful.

### About Development Mode

In daily development, you don't need to run the full `pnpm build` every time you make changes. OpenClaw provides two more efficient development approaches:

1. **Run TypeScript directly** (via `tsx`):

```bash
# pnpm openclaw internally uses tsx to execute TypeScript directly
pnpm openclaw gateway --port 18789
```

Behind the scenes, this calls `scripts/run-node.mjs`, which checks if `dist/` is outdated. If it is, it automatically rebuilds first, then runs.

2. **File watch mode** (detailed in the next section):

```bash
pnpm gateway:watch
```

We'll discuss these development loop tools in detail in section 2.4.
