TypeScript TS2451 “Cannot redeclare block-scoped variable” Across Files and How to Fix It
error TS2451: Cannot redeclare block-scoped variable 'count'. appears when TypeScript decides that a const or let with the same name in separate .ts files was declared twice in the same scope. A file with no import or export is treated as a "script" without its own file scope, so its variables are shared by all such files. Adding export {}; as one line to the file, or writing "moduleDetection": "force" in tsconfig.json, makes the error go away.
This is the cause and the fix for when tsc type checking or VS Code reports "Cannot redeclare block-scoped variable" even though the code is split across separate files. I reproduced the error in a test environment and included the message as it was printed. After the fix, I confirmed that the error goes away, using both tsc and the language server for editors.
The test environment was Ubuntu 24.04, Node.js 22.23.4, npm 10.9.9, and TypeScript 7.0.2. For the editor output, I ran the TypeScript 7 language server directly (tsc --lsp --stdio), not the editor itself such as VS Code. For browser behavior, I checked with Chromium 153 driven by Playwright. I did not test how the errors look in the VS Code UI, versions of TypeScript before 7, or builds that go through bundlers such as Vite.
- 1. Reproducing the error with the same const in two files
- 2. A file with no import or export shares its variables
- 3. Fix 1: add export {} to the file
- 4. Fix 2: write moduleDetection: force in tsconfig.json
- 5. Fix 3: rename the variable if the scripts are loaded on one page
- 6. No error in a project created with tsc –init
- 7. A variable named name causes an error even in a single file
Reproducing the error with the same const in two files
I ran npm init -y in an empty folder and installed TypeScript 7.0.2. I did not put "type" in package.json. I created tsconfig.json without moduleDetection, using the following content:
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}
I wrote a variable named count in both src/counter.ts and src/timer.ts. Neither file has an import or an export.
// src/counter.ts
const count = 1;
console.log(`counter: ${count}`);
// src/timer.ts
const count = 10;
console.log(`timer: ${count}`);
Running npx tsc --noEmit reported errors in both files and exited with code 1.
$ npx tsc --noEmit
src/counter.ts(1,7): error TS2451: Cannot redeclare block-scoped variable 'count'.
src/timer.ts(1,7): error TS2451: Cannot redeclare block-scoped variable 'count'.
With --pretty, the output also showed where the other file declares the same name (colors omitted). 'count' was also declared here. means that count is also declared at that location.
src/counter.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'count'.
1 const count = 1;
~~~~~
src/timer.ts:1:7 - 'count' was also declared here.
1 const count = 10;
~~~~~
src/timer.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'count'.
1 const count = 10;
~~~~~
src/counter.ts:1:7 - 'count' was also declared here.
1 const count = 1;
~~~~~
Found 2 errors in 2 files.
Errors Files
1 src/counter.ts:1
1 src/timer.ts:1
When I had the language server open src/counter.ts, it returned one diagnostic, with src/timer.ts attached as the related location. I used a small script of mine to reformat the server's responses to one diagnostic per line, with 1-based lines and columns. (I omitted the server name shown on the first line.) 1:7-1:12 is the range of the count on line 1 that the diagnostic applies to, and editors use this range to show the error marker. severity=1 is the value that the Language Server Protocol specification uses for errors.
diagnostics (1):
1:7-1:12 severity=1 source=ts code=2451 Cannot redeclare block-scoped variable 'count'.
related: src/timer.ts 1:7-1:12 'count' was also declared here.
Opening src/timer.ts gave the same result, with src/counter.ts as the related location. It is hard to see what the error conflicts with if you look at only one file. The "was also declared here" location tells you which other file is involved.
A file with no import or export shares its variables
The TypeScript Handbook's modules chapter says that a file with a top-level import or export is treated as a module, and a file with neither is treated as a script, whose contents are visible in the global scope. Variables in a script are declared in a scope shared by all such files. As a result, the const count in each separate file ends up as two declarations in the same scope.
This assumption matches the traditional way of loading files by listing them in <script> tags in HTML. The page explaining the module concept also says that all scripts loaded on the same page share one global scope.
I checked this in practice. With the error still present, I ran npx tsc to output JS. Both files were almost the same as the original .ts, except that "use strict"; was added at the top. I loaded them in the following HTML and opened it in Chromium.
<!doctype html>
<html>
<head>
<script src="dist/counter.js"></script>
<script src="dist/timer.js"></script>
</head>
<body></body>
</html>
The console output and the exception on the page were as follows. (In my script, exceptions are printed with the pageerror: prefix.)
console: counter: 1
pageerror: Uncaught SyntaxError: Identifier 'count' has already been declared
The first file ran, but the second file stopped with a SyntaxError saying that count had already been declared, and timer: 10 was never printed. TS2451 reports this runtime error during type checking.
Fix 1: add export {} to the file
If you intend the files to be separate modules, tell TypeScript so. The Handbook recommends adding a single export {}; line when you want a file with no import or export to be treated as a module.
I added it only to src/timer.ts.
const count = 10;
console.log(`timer: ${count}`);
export {};
npx tsc --noEmit printed nothing and exited with code 0. The language server returned zero diagnostics for both src/counter.ts and src/timer.ts. The count in src/timer.ts is now a variable local to that file, so it no longer conflicts with the count in src/counter.ts.
However, src/counter.ts is still a script, and its variable is still shared with other scripts. If you want every file to be a module, the next fix takes less effort than writing export {}; in each file.
Fix 2: write moduleDetection: force in tsconfig.json
I added "moduleDetection": "force" to compilerOptions in tsconfig.json. Do not forget the trailing comma on the previous line.
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"moduleDetection": "force"
},
"include": [
"src"
]
}
With the two original files still without export {};, npx tsc --noEmit exited with code 0, and the language server returned zero diagnostics for both src/counter.ts and src/timer.ts. According to the explanation of moduleDetection, "force" treats every file except declaration files (.d.ts) as a module.
The output JS had export {}; appended at the end, because TypeScript emits files it treats as modules that way.
const count = 1;
console.log(`counter: ${count}`);
export {};
By the Handbook's definition, a file with a top-level export is a module. This JS is therefore code to be loaded as a module, not a script loaded with <script src="..."> as in the HTML above. If your files are loaded with separate <script> tags, use the next fix.
Fix 3: rename the variable if the scripts are loaded on one page
If the files really are loaded with separate <script> tags, the variables are actually shared. In this case the error is not a configuration problem. It comes from using the same name twice.
When I renamed the variable in src/timer.ts to timerCount, npx tsc --noEmit exited with code 0. When I opened the output JS in the same HTML, there was no exception, and the console showed the following two lines.
console: counter: 1
console: timer: 10
No error in a project created with tsc –init
When I ran npx tsc --init with TypeScript 7.0.2, the generated tsconfig.json contained "module": "nodenext" and "moduleDetection": "force". With the same two files in this project, npx tsc --noEmit exited with code 0 and printed no errors.
Removing the "moduleDetection": "force" line gave the same result. npx tsc --showConfig displays the final configuration, and there moduleDetection was "force" even though I had not written it.
$ npx tsc --showConfig | grep -E "\"module\"|moduleDetection"
"module": "nodenext",
"moduleDetection": "force",
In the TypeScript 7 source code (typescript-go), when moduleDetection is not written, the default is force if module is in the range from node16 to nodenext, and auto otherwise.
When I ran with --moduleDetection auto, the same two TS2451 errors appeared in this project. I then ran npm pkg set type=module to add "type": "module" to package.json, and the errors went away even with auto. As described in the explanation of moduleDetection, when module is nodenext or node16, auto also checks whether "type" in package.json is "module" to decide whether a file is a module.
The results for two files that each declare const count, by setting, are summarized below.
| Setting | Result |
|---|---|
module is esnext, moduleDetection not written |
Error |
module is nodenext, moduleDetection not written |
No error |
module is nodenext, moduleDetection is auto, no "type" in package.json |
Error |
module is nodenext, moduleDetection is auto, "type": "module" in package.json |
No error |
moduleDetection is force |
No error |
A variable named name causes an error even in a single file
In a project with module set to esnext, I placed only the following src/greet.ts. No other file declares name.
const name = "neko";
console.log(`hello, ${name}`);
TS2451 still appeared. The first error is in lib.dom.d.ts, the browser type definitions that ship with TypeScript. (The path is from the Linux package of TypeScript 7 installed in the test environment.)
$ npx tsc --noEmit
node_modules/@typescript/typescript-linux-x64/lib/lib.dom.d.ts(43468,15): error TS2451: Cannot redeclare block-scoped variable 'name'.
src/greet.ts(1,7): error TS2451: Cannot redeclare block-scoped variable 'name'.
The relevant line in lib.dom.d.ts is declare const name: void;, which declares the browser global variable name. I did not set lib in this tsconfig.json. According to the explanation of lib, TypeScript includes the browser type definitions, such as document, by default. The name in src/greet.ts, which is treated as a script, conflicts with that declaration. In the language server diagnostics, the related location also pointed to the same line in lib.dom.d.ts.
For this file too, adding export {}; at the end made npx tsc --noEmit exit with code 0.





