TS2451: Cannot redeclare block-scoped variable TypeScript Origin: On your device OSI layer 7 · Application Last updated 5 Nov 2026

TypeScript: error TS2451: Cannot redeclare block-scoped variable 'name'.

A let/const/class name was declared twice in the same scope — often because files are global scripts sharing one scope.

Exact message you may see error TS2451: Cannot redeclare block-scoped variable 'name'.Cannot redeclare block-scoped variableTS2451 Cannot redeclare block-scoped variablets(2451) Cannot redeclare block-scoped variable 'x'error TS2451
Seen on: TypeScript

Meaning

If two .ts files without import/export declare the same top-level const, they share the global scope. Add export {} (making each a module), rename, or enable moduleDetection: "force" (TS 4.7+). Also check for lib.dom globals (name, status, top) — declaring const name in a script collides with window.name.

Common causes

  • Script (non-module) files with the same top-level name
  • Collision with DOM globals (name, length, event)
  • Duplicate declaration in one scope
  • Copy-pasted examples in a folder
  • ts-node running scripts

⚡ Quick fix

  1. Add export {}; to the file
  2. Set "moduleDetection": "force" in tsconfig
  3. Rename the variable

Detailed fix by platform

TypeScript

  1. export {}; // at the top level of each script file

How to diagnose

  1. Files — Modules?
  2. DOM — Global collision?
  3. Config — moduleDetection?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

already declared — SyntaxError: Identifier 'x' has already been declared (JavaScript)already declaredCannot find module './App.vue' or its corresponding type declarations — TypeScript: error TS2307: Cannot find module './App.vue' or its corresponding type declarations. (Vue)Cannot find module '.…ReferenceError — ReferenceError: x is not defined (JavaScript)ReferenceErrorTDZ — ReferenceError: Cannot access 'x' before initialization (JavaScript)TDZTS1192: Module has no default export — TypeScript: error TS1192: Module '"./utils"' has no default export. (TypeScript)TS1192: Module has no…TS1208: File cannot be compiled under '--isolatedModules' because it is considered a global script file — TypeScript: error TS1208: 'index.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement (TypeScript)TS1208: File cannot b…TS1259: Module can only be default-imported using the 'esModuleInterop' flag — TypeScript: error TS1259: Module '"express"' can only be default-imported using the 'esModuleInterop' flag (TypeScript)TS1259: Module can on…TS2300: Duplicate identifier — TypeScript: error TS2300: Duplicate identifier 'Foo'. (TypeScript)TS2300: Duplicate ide…TS2451: C…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.