tsc TS2451: Cannot redeclare block-scoped variable 'x' in CI
TS2451 means a block-scoped name (let/const) is declared more than once in the same scope. In CI it most often comes from a file with no imports/exports being treated as a global script that collides with another.
What this error means
tsc fails with "error TS2451: Cannot redeclare block-scoped variable 'config'." sometimes reported twice, once per declaration site across two files.
tsc
src/a.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'config'.
1 const config = loadA();
~~~~~~
src/b.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'config'.Common causes
Two script files share the global scope
A file with no top-level import/export is a global script, so its top-level const collides with the same name in another script file.
A genuine duplicate declaration in one scope
The same let/const name is declared twice within a single block or module.
How to fix it
Make the files modules
- Add an
importorexportso each file becomes a module with its own scope. - An empty
export {}is enough to convert a script to a module. - Re-run tsc to confirm the names no longer collide.
src/a.ts
// a.ts
export {}; // makes this a module, not a global script
const config = loadA();Remove a real duplicate declaration
When both declarations are in one scope, rename or delete one so the name is declared once.
How to prevent it
- Keep files as modules (with imports/exports) so top-level names are scoped.
- Add
export {}to any otherwise import-free file you intend as a module. - Avoid reusing the same top-level
constname across global scripts.
Related guides
tsc TS6133: 'x' is declared but its value is never read (noUnusedLocals) in CIFix tsc "error TS6133: 'x' is declared but its value is never read" in CI - noUnusedLocals or noUnusedParamet…
tsc TS1005: ';' expected (syntax error) in CIFix tsc "error TS1005: ';' expected" in CI - a syntax error where tsc could not parse a statement, often from…
tsc TS2459: Module 'x' declares 'y' locally, but it is not exported in CIFix tsc "error TS2459: Module 'x' declares 'y' locally, but it is not exported" in CI - you imported a name t…