โฌ๏ธ Migrating from Version 2
Miniflare v3 now uses workerd
, the
open-source Cloudflare Workers runtime. This is the same runtime that's deployed
on Cloudflareโs network, giving bug-for-bug compatibility and practically
eliminating behavior mismatches. Refer to the
Miniflare v3 and
Wrangler v3 announcements for more
information.
CLI Changes
Miniflare v3 no longer includes a standalone CLI. To get the same functionality, you will need to switch over to Wrangler. Wrangler v3 uses Miniflare v3 by default. To start a local development server, run:
$ npx wrangler@3 dev
If there are features from the Miniflare CLI you would like to see in Wrangler, please open an issue on GitHub.
API Changes
We have tried to keep Miniflare v3โs API close to Miniflare v2 where possible,
but many options and methods have been removed or changed with the switch to the
open-source workerd
runtime. See the
Getting Started guide for the new API docs.
Updated Options
kvNamespaces/r2Buckets/d1Databases
- In addition to
string[]
s, these options now acceptRecord<string, string>
s, mapping binding names to namespace IDs/bucket names/database IDs. This means multiple Workers can bind to the same namespace/bucket/database under different names.
- In addition to
queueBindings
- Renamed to
queueProducers
. This either accepts aRecord<string, string>
mapping binding names to queue names, or astring[]
of binding names to queues of the same name.
- Renamed to
queueConsumers
Either accepts a
Record<string, QueueConsumerOptions>
mapping queue names to consumer options, or astring[]
of queue names to consume with default options.QueueConsumerOptions
has the following type:interface QueueConsumerOptions {// https://developers.cloudflare.com/queues/platform/configuration/#consumermaxBatchSize?: number; // default: 5maxBatchTimeout?: number /* seconds */; // default: 1maxRetries?: number; // default: 2deadLetterQueue?: string; // default: none}
cfFetch
- Renamed to
cf
. Either accepts aboolean
,string
(as before), or an object to use a thecf
object for incoming requests.
- Renamed to
Removed Options
wranglerConfigPath/wranglerConfigEnv
- Miniflare no longer handles Wrangler's configuration. To programmatically
start up a Worker based on Wrangler configuration, use the
unstable_dev()
API.
- Miniflare no longer handles Wrangler's configuration. To programmatically
start up a Worker based on Wrangler configuration, use the
packagePath
- Miniflare no longer loads script paths from
package.json
files. Use thescriptPath
option to specify your script instead.
- Miniflare no longer loads script paths from
watch
- Miniflare's API is primarily intended for testing use cases, where file
watching isn't usually required. This option was here to enable Miniflareโs
CLI which has now been removed. If you need to watch files, consider using a
separate file watcher like
fs.watch()
orchokidar
, and callingsetOptions()
with your original configuration on change.
- Miniflare's API is primarily intended for testing use cases, where file
watching isn't usually required. This option was here to enable Miniflareโs
CLI which has now been removed. If you need to watch files, consider using a
separate file watcher like
logUnhandledRejections
- Unhandled rejections can be handled in Workers with
addEventListener("unhandledrejection")
.
- Unhandled rejections can be handled in Workers with
globals
- Injecting arbitrary globals is not supported by
workerd
. If you're using a service worker,bindings
will be injected as globals, but these must be JSON-serialisable.
- Injecting arbitrary globals is not supported by
https/httpsKey(Path)/httpsCert(Path)/httpsPfx(Path)/httpsPassphrase
- Miniflare does not support starting HTTPS servers yet. These options may be added back in a future release.
crons
workerd
does not support triggering scheduled events yet. This option may be added back in a future release.
mounts
Miniflare no longer has the concept of parent and child Workers. Instead, all Workers can be defined at the same level, using the new
workers
option. Here's an example that uses a service binding to increment a value in a shared KV namespace:import { Miniflare, Response } from "miniflare";const message = "The count is ";const mf = new Miniflare({// Options shared between workers such as HTTP and persistence configuration// should always be defined at the top level.host: "0.0.0.0",port: 8787,kvPersist: true,workers: [{name: "worker",kvNamespaces: { COUNTS: "counts" },serviceBindings: {INCREMENTER: "incrementer",// Service bindings can also be defined as custom functions, with access// to anything defined outside Miniflare.async CUSTOM(request) {// `request` is the incoming `Request` object.return new Response(message);},},modules: true,script: `export default {async fetch(request, env, ctx) {// Get the message defined outsideconst response = await env.CUSTOM.fetch("http://host/");const message = await response.text();// Increment the count 3 timesawait env.INCREMENTER.fetch("http://host/");await env.INCREMENTER.fetch("http://host/");await env.INCREMENTER.fetch("http://host/");const count = await env.COUNTS.get("count");return new Response(message + count);}}`,},{name: "incrementer",// Note we're using the same `COUNTS` namespace as before, but binding it// to `NUMBERS` instead.kvNamespaces: { NUMBERS: "counts" },// Worker formats can be mixed-and-matchedscript: `addEventListener("fetch", (event) => {event.respondWith(handleRequest());})async function handleRequest() {const count = parseInt((await NUMBERS.get("count")) ?? "0") + 1;await NUMBERS.put("count", count.toString());return new Response(count.toString());}`,},],});const res = await mf.dispatchFetch("http://localhost");console.log(await res.text()); // "The count is 3"await mf.dispose();
metaProvider
- The
cf
object andX-Forwarded-Proto
/X-Real-IP
headers can be specified when callingdispatchFetch()
instead. A defaultcf
object can be specified using the newcf
option too.
- The
durableObjectAlarms
- Miniflare now always enables Durable Object alarms.
globalAsyncIO/globalTimers/globalRandom
workerd
cannot support these options without fundamental changes.
actualTime
- Miniflare now always returns the current time.
inaccurateCpu
- Set the
inspectorPort: 9229
option to enable the V8 inspector. Visitchrome://inspect
in Google Chrome to open DevTools and perform CPU profiling.
- Set the
Updated Methods
setOptions()
- Miniflare v3 now requires a full configuration object to be passed, instead of a partial patch.
Removed Methods
reload()
- Call
setOptions()
with the original configuration object to reload Miniflare.
- Call
createServer()/startServer()
- Miniflare now always starts a
workerd
server listening on the configuredhost
andport
, so these methods are redundant.
- Miniflare now always starts a
dispatchScheduled()/startScheduled()
- The functionality of
dispatchScheduled
can now be done viagetWorker()
. For more information read the scheduled events documentation.
- The functionality of
dispatchQueue()
- Use the
queue()
method on service bindings or queue producer bindings instead.
- Use the
getGlobalScope()/getBindings()/getModuleExports()
- These methods returned objects from inside the Workers sandbox. Since
Miniflare now uses
workerd
, which runs in a different process, these methods can no longer be supported.
- These methods returned objects from inside the Workers sandbox. Since
Miniflare now uses
addEventListener()
/removeEventListener()
- Miniflare no longer emits
reload
events. As Miniflare no longer watches files, reloads are only triggered by initialisation orsetOptions()
calls. In these cases, it's possible to wait for the reload with eitherawait mf.ready
orawait mf.setOptions()
respectively.
- Miniflare no longer emits
Response#waitUntil()
workerd
does not support waiting for allwaitUntil()
ed promises yet.
Removed Packages
@miniflare/*
- Miniflare is now contained within a single
miniflare
package.
- Miniflare is now contained within a single