Error: invalid wrangler.toml" duplicate top-level key
Fixes 'Error: invalid wrangler.toml' caused by a duplicate top-level key. Use it when wrangler rejects the config after an edit, usually a doubled section header from copy-paste. The key trigger is the invalid wrangler.toml message naming a duplicate; the fix is merging the repeated keys.
TL;DR
TOML forbids defining the same key twice, and wrangler rejects the whole file when it finds one. The usual culprit is a copy-pasted section like a second [vars] block or two [env.staging] tables. Find the repeated key, merge the two blocks into one, and validate the file parses before deploying.
Error: invalid wrangler.toml: duplicate top-level key "vars"Steps
- List every top-level key and section header in the file and look for repeats:
grep -n '^\[' wrangler.toml
grep -n '^[a-z_]* =' wrangler.tomlExpected: each section header and each top-level key appears exactly once. Any line number pair showing the same name twice is the offender.
- Open the file around those line numbers and merge the duplicates into a single block. Keep all the settings, just under one header.
Expected: one [vars] block, one compatibility_date line, one table per environment name.
- Validate the file parses as TOML:
python3 -c "import tomllib; tomllib.load(open('wrangler.toml','rb')); print('valid')"Expected: the script prints "valid". If it errors, read the message; it names the offending key and line.
- Deploy:
wrangler deployExpected: config validation passes and the deploy proceeds.
Use this when
- Wrangler reports "invalid wrangler.toml" with a duplicate key
- The error appeared right after hand-editing or merging the config
- Two environment blocks share a name after a copy-paste
Not for this skill when
- The config is wrangler.jsonc; the duplicate idea is the same but the syntax and validator differ
- The error is about an unknown or misspelled key rather than a duplicate
- The deploy fails on bindings or auth, not on config parsing
Variant phrasings
- wrangler.toml duplicate key error
- invalid wrangler.toml duplicate top-level key vars
- wrangler config duplicate table header
Why it happens
TOML is strict: a key or table defined twice is a parse error, not a merge. It happens most often when environment sections are duplicated during copy-paste, when two people edit the file in a merge, or when a [vars] block gets added at the bottom while one already exists near the top.
Edge cases
- Wrangler does not support config inheritance or includes; do not try to work around the duplicate by splitting the file.
- A duplicate inside an
[env.name]table (twovarsunder the same env) fails the same way; check env-scoped sections too. - Comments and blank lines never cause this; only real keys and headers count.
- If the file is generated by a script, fix the generator or the duplicate comes back on the next run.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstdMxxd1Vy7GhSKa42H4FWw
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.