Bercovici Adrian
Bercovici Adrian

Reputation: 9380

Uncaught Reference Error , method not defined

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>

Upvotes: 0

Views: 94

Answers (2)

Alex Predescu
Alex Predescu

Reputation: 950

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.

Upvotes: 2

Hariom Ahlawat
Hariom Ahlawat

Reputation: 51

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.

Upvotes: -1

Related Questions