๐ Variables and Secrets
Bindings
Variable and secrets are bound as follows:
$ miniflare --binding KEY1=value1 --binding KEY2=value2 # or -b
wrangler.toml[vars]KEY1 = "value1"KEY2 = "value2"NUMBER = 42 # Note [vars] are automatically stringified
const mf = new Miniflare({ bindings: { KEY1: "value1", KEY2: "value2", },});
.env
Files
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:
$ miniflare --env .env.test # or -e
wrangler.toml[miniflare]env_path = ".env.test"
const mf = new Miniflare({ envPath: ".env.test",});
Text and Data Blobs
Text and data blobs can be loaded from files. File contents will be read and
bound as string
s and ArrayBuffer
s respectively.
$ miniflare --text-blob TEXT=text.txt --data-blob DATA=data.bin
wrangler.toml[text_blobs]TEXT = "text.txt"[data_blobs]DATA = "data.bin"
const mf = new Miniflare({ textBlobBindings: { TEXT: "text.txt" }, dataBlobBindings: { DATA: "data.bin" },});
Bindings Priority
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
)
Globals
You can also bind variables or arbitrary objects to the global scope, even in modules mode:
$ miniflare --global KEY1=value1 --global KEY2=value2
wrangler.toml[miniflare.globals]KEY1 = "value1"KEY2 = "value2"
const mf = new Miniflare({ globals: { KEY1: "value1", KEY2: "value2", FUNCTION: () => { ... } },});