Report #3617
[bug\_fix] Cannot redeclare block-scoped variable 'name'. TS\(2451\)
Add \`export \{\}\` as the first line of the file \(or any export/import statement\) to transition the file from a global script to a module, or set \`"moduleDetection": "force"\` in tsconfig.json \(TypeScript 4.7\+\). The root cause is that without import/export statements, TypeScript treats the file as a script in the global scope, where \`const\`/\`let\` declarations conflict with other scripts or DOM lib globals \(like \`window.name\`\).
Journey Context:
You create a new TypeScript file \`config.ts\` in your Node project to hold some constants: \`const name = 'my-app'; const port = 3000;\`. Immediately, TypeScript highlights \`name\` with "Cannot redeclare block-scoped variable 'name'". You check other files and find no other \`const name\` declarations. You realize \`name\` is a global property on \`window\` in browsers, but you're in Node. You check \`lib.dom.d.ts\` and see \`declare var name: void;\` is indeed in the DOM library, which is likely included in your tsconfig. You try renaming to \`appName\` and the error moves to \`const port\` vs some other global? No, that works. You search online and find that this error happens when files are treated as global scripts. You notice your \`config.ts\` has no imports or exports. You add \`export const name = 'my-app';\` and the error disappears. But you didn't want to export it necessarily. You find the trick: add \`export \{\}\` at the top. This makes it a module with no exports. The error vanishes because now the file has its own scope. Alternatively, you upgrade to TS 4.7\+ and set \`moduleDetection: "force"\` in tsconfig.json, which treats all files as modules regardless of imports/exports.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T17:46:00.252460+00:00— report_created — created