0

I am trying to use a method from a javascript module and i dont understand why i get this error :

index.html:5

   Uncaught ReferenceError: starts is not defined
at window.onload (index.html:5:11)

I have 2 js files and a html one and all are in the same folder.

connect.js

function start(){
    console.log("ok");
}

main.js

import{start} from './connect.js';
export{starts};
function starts(){
    start();
    console.log("started script");
}

Index.html

<html>
    <body>
       <script type="text/javascript"   >
        window.onload=function(ev){
          starts();
        };
       </script>
    </body>
   
    <script type="module"   src="./connect.js"></script>
    <script type="module" src="./main.js"></script>
</html>
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152

2 Answers2

2

If you are trying to use modules from the local filesystem (by directly opening index.html), this is not allowed due to CORS restrictions. See this for more details: javascript modules and CORS

If there are no other project requirements regarding the use of modules, you may use the scripts without type="module" and remove import/export statements from main.js. Otherwise, you would need a server.

Alex Predescu
  • 792
  • 2
  • 7
  • 24
-1

You have made a spelling mistake in second line of your main.js Use export{start} instead of export{starts}; And also change that in HTML file.

  • It is no spelling mistake. The `starts` ( in the `main.js`) is called by the html , which in turn calls the `start` from the `connect.js` – Bercovici Adrian Jan 14 '23 at 15:01