In this post, we will see how to resolve Why home is not defined?
Question:
I am not sure why home is not defined when i do this in app.js? I tried to reference home.js with app.js by adding codeIs that reference enough and should i focus my self to solve the second one error?
Index.html
Best Answer:
Please check the MDN Docs about importFrom Cannot use import statement outside a module in JavaScript
In JavaScript, a module is a code executed in its own scope, separate from the global scope. Import statements can only be used within a module and not in the global scope.
Fixing it for HTML
To fix the problem for HTML scripts, you need to set the element’s type attribute to specify the type of script included in the element.
If the type attribute is set to “module”, the browser will know that the script is a JavaScript module and will execute it as such.
This will allow you to use import statements within the script.
Here is an example:
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com