Skip to content

[html] "Cannot redeclare block-scoped variable" in script type module #169117

Open

Description

Does this issue occur when all extensions are disabled?: Yes

System:

  • VS Code Version: Version: 1.74.0
  • OS Version: macOS 13.0.1

Steps to Reproduce:

  1. Create a new HTML file
  2. Add two <script type="module"> and declare a variable with the same name in both (see example code below).
  3. Observe the red squiggly line underneath the variable names saying Cannot redeclare block-scoped variable.
image

When a script tag has the type attribute set to "module", the source text should be treated as an ES module with it's own scope, not the global scope.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts

Example HTML

<!DOCTYPE html>
<script type="module">
  const x = 1;
</script>
<script type="module">
  const x = 2;
</script>

Activity

  1. changed the title [-]Cannot redeclare block-scoped variable in script type module[/-] [+]"Cannot redeclare block-scoped variable" in script type module[/+] on Dec 14, 2022
  2. vscodenpa commented on Dec 14, 2022

    @vscodenpa

    Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.74.0. Please try upgrading to the latest version and checking whether this issue remains.

    Happy Coding!

  3. marlun78 commented on Dec 14, 2022

    @marlun78
    Author

    Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.74.0. Please try upgrading to the latest version and checking whether this issue remains.

    Yes, it is still a valid issue.

  4. changed the title [-]"Cannot redeclare block-scoped variable" in script type module[/-] [+][html] "Cannot redeclare block-scoped variable" in script type module[/+] on Dec 14, 2022
  5. added
    bug
    html
    on Dec 14, 2022
  6. Eghizio commented on Jul 15, 2024

    @Eghizio

    Would be lovely if this would have been taken care of.
    Or at least show us how we could fix/patch it locally 🙏

  7. twx0504 commented on Oct 30, 2025

    @twx0504

    HI, this issue is still present. May I know if there's any update or fix we can do?

  8. aeschli commented on Oct 31, 2025

    @aeschli
    Contributor

    Help is always welcome. The code is at

    function getEmbeddedDocument(document: TextDocument, contents: EmbeddedRegion[], languageId: string, ignoreAttributeValues: boolean): TextDocument {

    We create a document that only consist of the JavaScript parts. Likely we need to add {/} for each script part of type module.

  9. added
    help wanted
    on Oct 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bughelp wantedhtml

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions