workerd Runtime

1.20260421.1 · active · verified Tue Apr 21

Cloudflare's `workerd` is an open-source JavaScript/WebAssembly server runtime, built on the same core technology that powers Cloudflare Workers. It provides a local execution environment for developing and testing serverless applications that replicate the Cloudflare Workers platform. The current stable version, `v1.20260421.1`, follows a `v1.YYYYMMDD.X` daily release cadence, reflecting continuous integration and frequent updates with features and bug fixes. Key differentiators include its high compatibility with the Cloudflare Workers ecosystem, enabling seamless local development and deployment, and its foundation on the proven, high-performance Cloudflare infrastructure. Unlike traditional Node.js runtimes, `workerd` is designed for a secure, isolated, and highly concurrent serverless model, with built-in support for web standards like `fetch` and `Durable Objects`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and run a simple Cloudflare Worker locally using the `workerd` runtime, involving a JavaScript worker script and a Cap'n Proto configuration file.

/*
1. Create your worker script: worker.js
   Save this content to a file named `worker.js` in your project directory.
*/
export default {
  async fetch(request) {
    const url = new URL(request.url);
    if (url.pathname === '/greet') {
      const name = url.searchParams.get('name') || 'World';
      return new Response(`Hello, ${name} from workerd!`, {
        headers: { 'Content-Type': 'text/plain' },
        status: 200
      });
    }
    return new Response('Not Found. Try visiting /greet?name=Alice', { status: 404 });
  }
};

/*
2. Create your workerd configuration file: workerd.capnp
   This file tells the `workerd` runtime how to load and serve your worker.
   Save this content to a file named `workerd.capnp` in the same directory.
*/
// using Workerd = /capnp/workerd.capnp;
//
// const config :Workerd.Config = (
//   services = [
//     (name = "main", worker = .worker),
//   ],
//   bundles = [
//     (name = "my-worker-bundle", content = embed "worker.js"), // Reference your `worker.js` file
//   ],
//   sockets = [
//     (name = "http", address = "0.0.0.0:8080", service = "main"), // workerd listens on port 8080
//   ]
// );
//
// const worker :Workerd.Worker = (
//   serviceWorkerScript = .myWorkerBundle,
//   compatibilityDate = "2023-01-01", // Required by Cloudflare Workers for consistent behavior
//   compatibilityFlags = ["nodejs_compat"], // Example: Enable Node.js compatibility features
// );

/*
3. Install workerd and run your worker
   First, ensure you have Node.js and npm/npx installed.
   Then, in your terminal, navigate to your project directory and run:

   npm install workerd
   npx workerd serve workerd.capnp

   After running, open your web browser and visit:
   - http://localhost:8080/greet             (Expected: "Hello, World from workerd!")
   - http://localhost:8080/greet?name=Alice (Expected: "Hello, Alice from workerd!")
*/

view raw JSON →