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.
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.
// 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.