order |
---|
2 |
Variable and secrets are bound as follows:
const mf = new Miniflare({
bindings: {
KEY1: "value1",
KEY2: "value2",
},
});
Variables and secrets are automatically loaded from a .env
file in the current
directory. This is especially useful for secrets if your .env
file is
.gitignore
d. .env
files look something like this:
KEY1=value1
# Woah, comments!
KEY2=value2
You can also specify the path to a custom .env
file:
const mf = new Miniflare({
envPath: ".env.test",
});
Text and data blobs can be loaded from files. File contents will be read and
bound as string
s and ArrayBuffer
s respectively.
const mf = new Miniflare({
textBlobBindings: { TEXT: "text.txt" },
dataBlobBindings: { DATA: "data.bin" },
});
Higher priority bindings override lower priority bindings with the same name. The order (from lowest to highest priority) is:
- Variables from
wrangler.toml
[vars]
- Variables from
.env
files - WASM module bindings (
--wasm
,[wasm_modules]
) - Text blob bindings (
--text-blob
,[text_blobs]
) - Data blob bindings (
--data-blob
,[data_blobs]
) - Custom bindings (
--binding
,bindings
)
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.
Miniflare will always set the global variable MINIFLARE
to true
in its
sandbox. You can use this as an escape hatch to customise behaviour during local
development:
if (globalThis.MINIFLARE) {
// Do something when running in Miniflare
} else {
// Do something else when running in the real Workers environment
}