Skip to main content
The Worker struct defines the code, compatibility settings, bindings, and storage configuration for a JavaScript or WebAssembly worker.

Code definition

modules
List<Module>
ES modules that compose the worker. The first module is the main module which exports event handlers.
serviceWorkerScript
Text
Alternative to modules: a single script using global addEventListener() to register event handlers (service worker syntax).Use Cap’n Proto’s embed directive to read from an external file:
serviceWorkerScript = embed "worker.js"
inherit
Text
Inherit configuration from another worker by service name. Creates a clone with modifiable bindings and globalOutbound, but compatibility settings cannot be changed.

Module types

modules[].name
Text
required
Name or path used to import the module.
modules[].esModule
Text
ES module file with imports and exports.
modules[].commonJsModule
Text
CommonJS module using require().
modules[].text
Text
Raw text blob. Importing produces a string.
modules[].data
Data
Raw data blob. Importing produces an ArrayBuffer.
modules[].wasm
Data
Compiled binary WebAssembly module. Importing produces a WebAssembly.Module object.
modules[].json
Text
JSON data. Importing produces the parsed result.
modules[].pythonModule
Text
Python module. All bundles containing this type are converted into a JS/WASM worker bundle prior to execution.
modules[].pythonRequirement
Text
Python package required by the bundle. Must be supported by Pyodide. Only the module name matters; the field value is ignored.

Compatibility

compatibilityDate
Text
required
Compatibility date in YYYY-MM-DD format. Required unless inheriting from another worker.See Cloudflare Workers compatibility dates for details.
compatibilityFlags
List<Text>
Optional compatibility flags to enable specific behaviors.

Bindings

bindings
List<Binding>
Bindings give the worker access to external resources and configuration settings.For ES modules syntax, bindings are delivered via the env object. For service workers syntax, each binding appears as a global variable.
See Binding schema for detailed binding types.

Outbound configuration

globalOutbound
ServiceDesignator
default:"internet"
Where the global fetch() function sends requests. Defaults to the “internet” service.
cacheApiOutbound
ServiceDesignator
Where Cache API requests (caches.default, caches.open()) are sent.

Durable Objects

durableObjectNamespaces
List<DurableObjectNamespace>
List of Durable Object namespaces in this worker.
durableObjectNamespaces[].className
Text
required
Exported class name implementing the Durable Object. Can be changed without breaking compatibility if uniqueKey stays the same.
durableObjectNamespaces[].uniqueKey
Text
Unique, stable ID for this namespace (e.g., a GUID). Used to derive object IDs cryptographically.DO NOT LOSE this key, or stored data may become inaccessible.
durableObjectNamespaces[].ephemeralLocal
Void
Marks instances as ephemeral with no durable storage. The state.storage API will not be present.
durableObjectNamespaces[].preventEviction
Bool
default:"false"
Keep Durable Objects pinned to memory forever. Only supported in workerd; not available in production.
durableObjectNamespaces[].enableSql
Bool
default:"false"
Allow Durable Objects in this namespace to use the storage.sql API for SQL queries.
durableObjectStorage
union
Specifies where this worker’s Durable Objects are stored.Options:
  • none: No Durable Objects (default)
  • inMemory: Store in memory only (for testing)
  • localDisk: Store in a directory on disk (experimental)
durableObjectUniqueKeyModifier
Text
Additional text hashed with uniqueKey for inherited workers. Each derived worker must specify a unique modifier.DO NOT LOSE this value.

Example

worker = (
  modules = [
    ( name = "main.js",
      esModule = embed "main.js"
    )
  ],
  compatibilityDate = "2024-01-01",
  compatibilityFlags = ["nodejs_compat"],
  bindings = [
    ( name = "DB",
      durableObjectNamespace = "MyDatabase"
    ),
    ( name = "API_KEY",
      text = "secret-key-value"
    )
  ],
  durableObjectNamespaces = [
    ( className = "MyDatabase",
      uniqueKey = "550e8400-e29b-41d4-a716-446655440000"
    )
  ]
)

Build docs developers (and LLMs) love