javascripthtmlattributes

Error: A script element with a defer attribute must not have a type attribute with the value module


I have this code in the <head>-section:

<script defer src="./static/js/main.js" type="module"></script>

The validator on validator.w3.org gives me the error: "A script element with a defer attribute must not have a type attribute with the value module."

The javascript works fine and I am trying to understand the reason for the error. What can I do to solve the error?


Solution

  • I found the answer on https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script.

    The defer attribute has no effect on module scripts — they defer by default.

    The error message is a little bit confusing. It suggests there is something wrong with the type-attribute. It would be a little bit more clear if it would say something like: "A script element with a type attribute with the the value module should not have a defer attribute."