0

catalog

 - folder
  - index.html
  - index.js
  - index1.js

index.html

   <!-- main content --->
   <script type='module' src='./index.js'></script>

index.js

   import {show} from "./index1"

   show();

index1.js

   export default function show(){
       console.log('hello world');
   }

descripttion

when run index.html on browser, is don't work, and i got this error:

Access to script at 'file:///Users/xxx/Documents/code/canvas/test/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

browser error

it may work with server, how to make it work without server, moulti-module.

import a module from other. just like run with nodejs env.

Sourabh Somani
  • 2,002
  • 1
  • 9
  • 23
卡西莫多
  • 23
  • 1
  • 6
  • 1
    Does this answer your question? [Access to Image from origin 'null' has been blocked by CORS policy](https://stackoverflow.com/questions/41965066/access-to-image-from-origin-null-has-been-blocked-by-cors-policy) – Sourabh Somani Dec 18 '19 at 06:36

1 Answers1

0

Importing a script as a module: File should export as module.exports

Please refer to the below link for working example: https://stackblitz.com/edit/js-mgwfrd

Rahul Sharma
  • 198
  • 1
  • 7
  • I am sorry, your demo run correct, but not work at localhost, the error msg `Access to script at 'file:///Users/br/Documents/code/demo_canvas/demo10/fileHandler.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.` – 卡西莫多 Dec 20 '19 at 02:17