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.
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 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
- Add
export {};to the file - Set
"moduleDetection": "force"in tsconfig - Rename the variable
Detailed fix by platform
TypeScript
export {}; // at the top level of each script file
How to diagnose
- Files — Modules?
- DOM — Global collision?
- 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:
Similar errors
Most viewed in TypeScript
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026