0

I am making a simple test project including 2 JavaScript files , where i export and import modules , as shown below .

However ,when opening html page , an error generated in the console shows that :

 Access to script at 'file:///C:/Users/index2.js' from origin 'null' has been blocked by CORS policy 
Failed to load resource: net::ERR_FAILED      (index2.js:1)

I tried to desactivate CORS but that leads always to the same error , i am using Google Chrome browser .

what is abnormal in code and what to do to resolve this problem ?

index2.js :

export default class test {

 static method () {
   return ('hello world' ) ; 
}

index.js :

import test from './index2.js'; 

console.log (test.method())  ; 

in index.html :

<script type = "module" src="./index.js"></script>
Hamdi
  • 51
  • 2
  • 11
  • are you using any build tool? webpack etc.? – aviya.developer Aug 18 '19 at 21:48
  • Most browsers don't allow you to access files on the local filesystem using JS. You will need to setup a local webserver and access the files from there. Check out this post: https://stackoverflow.com/q/50197495/5347875 – djs Aug 18 '19 at 21:50
  • aviya.developer no i didn't use a build tool in this example , @Daniel.Schroeder any suggestions ? thanks . – Hamdi Aug 18 '19 at 22:42

1 Answers1

-1

I resolved the problem by setting up a local webserver , I Used XAMPP , the link below shows the installation and usage :

https://www.maketecheasier.com/setup-local-web-server-all-platforms/

Hamdi
  • 51
  • 2
  • 11
  • XAMPP is arguably the wrong solution. It's a giant product that requires configuration and only serves specific preconfigured directories. [Use one of these](https://stackoverflow.com/questions/12905426/what-is-a-faster-alternative-to-pythons-http-server-or-simplehttpserver). They are lightweight and serve any folder instantly. No experienced web developer would use XAMPP for this situation. – gman Aug 19 '19 at 00:16