Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Query regarding import and export module in JavaScript

gyansangrah

New Coder
Hi Experts

I want to implement import and export module. For that, I have created a file "ExportClass.js". Here is the code

JavaScript:
    const person = {
        name: 'Max'
    };
    
    export default person;

I am created another file "ImportClass.js" to import this module


JavaScript:
    // JavaScript source code
    import { person } from './ExportClass.js';
    
    
    function Test() {
        alert(person.name);
        //exportClass.Print1();
    }

After creating both JavaScript files, I have created the HTML file.

HTML:
    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script type="module" src="ImportClass.js"></script>
        <script>
            Test();
        </script>
    </head>
    <body>
    
    </body>
    </html>

But the module is not called. Please help.
 
Should this:-

[CODE title="Jav"]<script type="module" src="ImportClass.js"></script>[/CODE]

Not read like this:-

[CODE title="Javascript"]<script type="module" src="./ImportClass.js"></script>[/CODE]

Also, open Console in dev tools and copy/paste any error messages
 
Here is the error on console

HTMLPage1.html:9 Uncaught ReferenceError: Test is not defined at HTMLPage1.html:9
HTMLPage1.html:1 Access to script at 'file:///C:/temp/ImportClass.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
ImportClass.js:1 Failed to load resource: net::ERR_FAILED
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom