Basic worker structure
A worker definition includes the code to execute, compatibility settings, and optional bindings:Worker code formats
You can define worker code in three ways:ES modules (recommended)
Use themodules field to define one or more ES modules. The first module in the list is the main module that exports event handlers.
Service Worker script
UseserviceWorkerScript for a single script that uses global addEventListener() to register event handlers:
Inherited worker
Inherit from another worker to create a clone with modified settings:bindings: Override specific named bindings (must match name and type in base)globalOutbound: Override the outbound servicedurableObjectStorage: Required if base defines Durable Object namespacesdurableObjectUniqueKeyModifier: Required for derived workers with Durable Objects
Module types
Each module in themodules list has a name and content:
Module name or path used for imports. For example,
"index.js" or "./utils/helpers.js".ES module with imports and exports. Use
embed to include external files:CommonJS module using
require(). Use with namedExports to declare expected exports:Raw text blob. Importing produces a string:
Raw binary data. Importing produces an ArrayBuffer:
WebAssembly module. Importing produces a
WebAssembly.Module object:JSON data. Importing produces the parsed JavaScript object:
Python module. All bundles containing Python modules are converted to JS/WASM bundles prior to execution.
Python package from Pyodide. The package is installed before worker execution. Value should be an empty string; only the module name matters.
Compatibility settings
workerd maintains backwards compatibility through compatibility dates and flags. Your code will always behave as it did on your specified date, even when workerd is updated.The date that determines which API behaviors your worker uses. Format: See Compatibility dates for available flags.
"YYYY-MM-DD".This must be specified unless the worker inherits from another worker.Optional flags to enable specific features or behaviors:Common flags:
"nodejs_compat": Enable Node.js compatibility APIs"nodejs_compat_v2": Enhanced Node.js compatibility"experimental": Enable experimental features"no_global_navigator": Remove globalnavigatorobject"streams_enable_constructors": Enable ReadableStream/WritableStream constructors
Outbound networking
Service that handles the global
fetch() function. Defaults to the “internet” service.Service that handles Cache API requests (
caches.default, caches.open()).Durable Objects
Configure Durable Object namespaces for stateful, coordinated workers:List of Durable Object classes defined in this worker.
Durable Object namespace fields
Exported class name that implements the Durable Object. Changing the class name doesn’t break compatibility with existing storage if
uniqueKey stays the same.Unique, stable ID for this namespace. Can be any string (GUID, random hex, etc.). This key:
- Ensures objects have unique identifiers distinct from other classes
- Is used for cryptographic derivation and validation of object IDs
- Keeps IDs secret if you want to prevent forgery
Make instances ephemeral with no durable storage. The
state.storage API is not present. These objects:- Allow arbitrary strings as IDs
- Have no
idFromName()ornewUniqueId()methods - Are unique per-colo on Cloudflare’s network, not globally
Keep Durable Objects pinned in memory forever. By default, objects are evicted after 10 seconds of inactivity.
This is only supported in workerd, not in production Durable Objects.
Enable the
storage.sql API for executing SQL queries. workerd uses SQLite to back all Durable Objects, but the SQL API is hidden by default.Attach containers to Durable Objects in this namespace. workerd talks to the configured container engine to start containers based on the given image.
Durable Object storage
Specifies where Durable Objects are stored.
none: Worker has no Durable Objects, or defines onlyephemeralLocalnamespacesinMemory: Stores in-memory for the lifetime of the process (testing only)localDisk: Stores in a local directory (experimental, subject to change)
Additional text hashed with
DurableObjectNamespace.uniqueKey. Required when using worker inheritance so each derived worker has unique Durable Object IDs.Tail workers
List of tail worker services that receive tail events for this worker. See Tail Workers.
Container engine
Configure container operations for Durable Objects with attached containers:Complete example
Next steps
Bindings
Grant capabilities through bindings
Services
Learn about service types