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

Javascript file with module import does not work

I’m doing a small learning project on and came across the fact that when I add import to a js script, it seems to start being ignored and not executed.
I don’t quite understand if the problem is in the code or in WebStorm.

I wrote a simplified example, but it didn’t work either.

test.html

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>Hello world!</div>
<div id="display"></div>
<script type="module" src="./main.js"></script>
</body>
</html>

main.js

import {print} from "./func";
print();

func.js

export function print(){
    document.getElementById("display").innerHTML += "Hello";
}

All files are in the same folder.

>Solution :

Your import statement in main.js is just slightly off. You should change

import { print } from "./func";

to:

import { print } from "./func.js";
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