Durable Objects (2)

Last updated Jan 28, 2026 - Durable Objects Reference - Using Durable Objects

Objects

Specify Durable Objects to add to your environment as follows:

const mf = new Miniflare({
    modules: true,
    script: `
  export class Object1 {
    async fetch(request) {
      ...
    }
  }
  export default {
    fetch(request) {
      ...
    }
  }
  `,
    durableObjects: {
        // Note Object1 is exported from main (string) script
        OBJECT1: "Object1",
    },
});

Persistence

By default, Durable Object data is stored in memory. It will persist between reloads, but not different Miniflare instances. To enable persistence to the file system, specify the Durable Object persistence option:

const mf = new Miniflare({
    durableObjectsPersist: true, // Defaults to ./.mf/do
    durableObjectsPersist: "./data", // Custom path
});

Manipulating Outside Workers

For testing, it can be useful to make requests to your Durable Objects from outside a worker. You can do this with the getDurableObjectNamespace method.

import { Miniflare } from "miniflare";

const mf = new Miniflare({
    modules: true,
    durableObjects: { TEST_OBJECT: "TestObject" },
    script: `
  export class TestObject {
    constructor(state) {
      this.storage = state.storage;
    }

    async fetch(request) {
      const url = new URL(request.url);
      if (url.pathname === "/put") await this.storage.put("key", 1);
      return new Response((await this.storage.get("key")).toString());
    }
  }

  export default {
    async fetch(request, env) {
      const stub = env.TEST_OBJECT.getByName("test");
      return stub.fetch(request);
    }
  }
  `,
});

const ns = await mf.getDurableObjectNamespace("TEST_OBJECT");
const stub = ns.getByName("test");
const doRes = await stub.fetch("http://localhost:8787/put");
console.log(await doRes.text()); // "1"

const res = await mf.dispatchFetch("http://localhost:8787/");
console.log(await res.text()); // "1"

Using a Class Exported by Another Script

Miniflare supports the script_name option for accessing Durable Objects exported by other scripts. This requires mounting the other worker as described in 🔌 Multiple Workers.

Was this helpful?

YesNo

On this page

Durable Objects (2) - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/workers/testing/miniflare/storage/durable-objects/#page","headline":"Durable Objects","description":"Specify Durable Objects to add to your environment as follows:","url":"https://developers.cloudflare.com/workers/testing/miniflare/storage/durable-objects/","inLanguage":"en","image":"https://developers.cloudflare.com/workers/testing/miniflare/storage/durable-objects/og.png?v=530431b367eec2e3","dateModified":"2026-01-28","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: "Specify KV namespaces to add to your environment as follows:" title: KV image: https://developers.cloudflare.com/workers/testing/miniflare/storage/kv/og.png?v=4876bc91e9008329


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/workers/llms.txt
Use this file to discover all available pages before exploring further.