Back to Blog
Frontend Development
November 4, 2025
Soumya Rauth

Cannot redeclare block-scoped variable. The Reason behind the error and the way to resolve it.

The error message "Cannot redeclare block-scoped variable" typically occurs in JavaScript when a variable with the same name is declared more than once within the same block scope, such as within a function or a block of code enclosed in curly braces. This violates the rule of variable declaration within block scope, where each variable name should be unique within that scope.

Few days back I was trying to write a script in my Visual Studio Code IDE, where I was trying to declare and initialize a variable with the keyword let.

But, I was faced with an error. Part of the error message was, Cannot redeclare block-scoped variable.

// main.ts
let var1 = "hello";

let var1: string; // ❌ Error: Cannot redeclare block-scoped variable 'var1'

So, I dug a bit into it to find out the reason behind the error. What I found was:

  1. Because, the let keyword is block scoped and unlike var, it can not be re-declared or reassigned within the same block.
  2. After Transpiling , the var1 variable of my main.ts file is also now available on the Transpiled main.js file as “var var1”.
  3. Right now after the Transpilation there are essentially two variables with the same name var1; one of which is in the main.ts which is declared as let (remember let is not redeclared or reassigned) and another one is in main.js
  4. So naturally visual studio code displays the error on main.ts file because the variable in there, is declared with the let keyword.
  5. As a result it won’t allow to Transpile the file and displays Cannot redeclare block-scoped variable “variable name”.

How to resolve ?

Typescript is moduler and each module has it’s own block. So, basically if you could somehow enclose the variable which is declared as let within it's own module, the error will be resolved because now your let variable is in it’s separate  block.

To achieve this, simply type export on the top of your script or additionally you can type export {}. Now you will see that the error is resolved.

// main.ts
let var1: string = "hello"; // ✔ Declare once with type

Hope this article helps you finding out and resolving the issue. Thanks a bunch! for reading out.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript