Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'import' statement not working in script in rendered HTML

I'm developing a express application and Im trying to do the below thing and I keep getting the same error over and over - "GET http://localhost:3000/javascript/module2 net::ERR_ABORTED 404 (Not Found)"

Could someone please point out to me what mistake I am committing and point me in the right direction? Below is my code -

Module1.js

import theFunc from "./module2";
console.log("In module 1");
theFunc();    

Module2.js

export default function theFunc() {
console.log("from module 2");
}

index.html

<html>
 <body>
   Should show the result in console.
  <script type="module" src="./javascript/module1.js"></script>
 </body>
</html>

Thanks a ton in advance! :)

like image 428
Sk_Joat Avatar asked Mar 03 '26 14:03

Sk_Joat


1 Answers

The message points out the issue, but it's still pretty easy to miss.

GET http://localhost:3000/javascript/module2 net::ERR_ABORTED 404

is the issue, because instead of

http://localhost:3000/javascript/module2

the URL is actually

http://localhost:3000/javascript/module2.js

with a .js extension.

The issue is that

import theFunc from "./module2";

should be

import theFunc from "./module2.js";
like image 94
loganfsmyth Avatar answered Mar 05 '26 03:03

loganfsmyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!