Home › JavaScript

TS2451: Cannot redeclare block-scoped variable (TypeScript) — how to fix

error TS2451: Cannot redeclare block-scoped variable 'name'.
index.ts(1,7): error TS2451: Cannot redeclare block-scoped variable 'express'.

Quick fix: Make the file a module by adding an import or export {}; at the top, or set "moduleDetection": "force" in tsconfig.json. If the name clashes with a DOM global like name, rename it. Otherwise remove the duplicate let/const in the same scope.

What it means

let and const can only be declared once per scope. TypeScript reports TS2451 when it sees a second declaration of the same name. The surprising part: a .ts file with no import or export is a script, and all scripts share one global scope. So const express = require('express') in two files is a redeclaration.

Cause 1: files without import/export are global scripts

// a.ts
const express = require("express");

// b.ts
const express = require("express"); // TS2451

Fix: turn each file into a module. Any top-level import or export does it; if you have none, add:

export {};

Or switch to ES imports, which also makes the file a module:

import express from "express";

Since TypeScript 4.7 you can make every file a module in tsconfig.json:

{
  "compilerOptions": {
    "moduleDetection": "force"
  }
}

Cause 2: the name collides with a DOM global

With the default lib (which includes dom), names such as name, status, close, top, event and length are already declared globally. In a script file:

const name = "Ada"; // TS2451: Cannot redeclare block-scoped variable 'name'

Fix: make the file a module (Cause 1) or rename the variable. For a Node project that does not need browser types, drop dom from lib:

{ "compilerOptions": { "lib": ["ES2022"] } }

Cause 3: real duplicate in the same scope

let total = 0;
// ...
let total = 10; // TS2451

Assign instead of redeclaring (total = 10;), rename, or move one into its own block. The same applies to switch cases, which share one scope; wrap the case body in braces:

switch (kind) {
  case "a": { const msg = "A"; break; }
  case "b": { const msg = "B"; break; }
}

Cause 4: compiled .js files are included too

If tsc output lands next to the sources and allowJs is on, both index.ts and index.js are compiled and declare the same globals. Set outDir and exclude it:

{
  "compilerOptions": { "outDir": "dist" },
  "include": ["src"]
}

Last updated 2026-12-01.