Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

import statement in javascript

I have html code with javascript inside a django project:

<body>
<-- some html>
</body>
<script>
import fs from "fs"
// some javascript
</script>

Now I get the error import declarations may only appear at the top level of a module.

What am I doing wrong? And is there some reference that explains how the import works in javascript? I’m not used to javascript yet and in all other languages I never had an issue with import statements.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By default, <script> tags are interpreted as regular scripts, not as ES6 modules. Specify the script as a module using the type="module" attribute.

<body>
<-- some html>
</body>
<script type="module">
import fs from "fs";
/* fs cannot be imported in browser environment
since it is a Node.js standard lib */
// some javascript
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading