Get Started
The Miniflare API allows you to dispatch events to workers without making actual HTTP requests, simulate connections between Workers, and interact with local emulations of storage products like KV, R2, and Durable Objects. This makes it great for writing tests, or other advanced use cases where you need finer-grained control.
Miniflare is installed using npm
as a dev dependency:
In all future examples, we'll assume Node.js is running in ES module mode. You
can do this by setting the type
field in your package.json
:
To initialise Miniflare, import the Miniflare
class from miniflare
:
The rest of these docs go into more detail on configuring specific features.
Note in the above example we're specifying script
as a string. We could've
equally put the script in a file such as worker.js
, then used the scriptPath
property instead:
Miniflare's API is primarily intended for testing use cases, where file watching isn't usually required. If you need to watch files, consider using a separate file watcher like fs.watch() ↗ or chokidar ↗, and calling setOptions() with your original configuration on change.
To cleanup and stop listening for requests, you should dispose()
your instances:
You can also manually reload scripts (main and Durable Objects') and options by calling setOptions()
with the original configuration object.
You can use the setOptions
method to update the options of an existing
Miniflare
instance. This accepts the same options object as the
new Miniflare
constructor, applies those options, then reloads the worker.
getWorker
dispatches fetch
, queues
, and scheduled
events
to workers respectively:
See 📨 Fetch Events and ⏰ Scheduled Events for more details.
Miniflare starts an HTTP server automatically. To wait for it to be ready, await
the ready
property:
By default, Miniflare will fetch the Request#cf
object from a trusted
Cloudflare endpoint. You can disable this behaviour, using the cf
option:
You can also provide a custom cf object via a filepath:
To start an HTTPS server instead, set the https
option. To use the default shared self-signed certificate ↗, set https
to true
:
To load an existing certificate from the file system:
To load an existing certificate from strings instead:
If both a string and path are specified for an option (e.g. httpsKey
and
httpsKeyPath
), the string will be preferred.
By default, [mf:*]
logs are disabled when using the API. To
enable these, set the log
property to an instance of the Log
class. Its only
parameter is a log level indicating which messages should be logged: