13

I'm writing JavaScript for the browser, my script.js has something like

import { foo, bar } from "./lib/sth.js"
function main() { ... }

Then I have this in my browser:

<script type=module src="./script.js"></script>
<body onload="main();"> ... </body>

But it's keep giving me this error:

Uncaught ReferenceError: main is not defined at onload ((index):7)

Why is my main now defined? It works fine before I use type=module, but with the import statement, I believe it has to be type=module

8
  • If I am not mistaken, import is not supported natively by browsers. Do you use any compiler to compile your es6 code to plain-old js so that browser can understand it? I think that your whole script.js is not properly loaded because of that. It is not just the main() function. Do you see any other errot before that? Commented Oct 12, 2018 at 18:24
  • @AlkisMavridis Sure it is: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 12, 2018 at 18:25
  • 1
    it doesn't look like script.js is a module Commented Oct 12, 2018 at 18:25
  • 5
    Module scope != global scope. If you want main to be globally available, you need to do this yourself: window.main = main;. Commented Oct 12, 2018 at 18:26
  • 2
    @Royson: Quotes around attribute values are optional in HTML, if the value consists only specific "safe" characters. Commented Oct 12, 2018 at 18:29

2 Answers 2

10

Thanks for @HereticMonkey and @FelixKling!

window.onload = function() { ... } 

does work for my problem. Yet I'm confused why import is designed like this. Suppose I just wanna use some library in my script so I import it, why this makes my script has to be a module as well?

Sign up to request clarification or add additional context in comments.

6 Comments

why this makes my script has to be a module as well? well, because you wrote <script type="module" ...>
@PatrickRoberts but if I use import statement without use type=module will cause an syntax error in the browser
Yes I'm aware, but import can only be used in a module scope. That's the whole point.
That's the part I'm confused, if I use a metaphor in C/C++, I would say module is like ".h", and my scripts is just a regular ".c" file, it does make sense to me if I'm using a ".h" then my own file must be ".h" as well
Your metaphor doesn't make sense either. A .h file is a declaration, while a .c is an implementation. It's generally bad practice to declare things in JavaScript without defining them in the same scope. Doing so is what leads to things like callback hell and questions that get closed as a duplicate of this or this.
|
3

In JavaScript, module scope is not the same as global scope. When using ES6 modules (e.g., import and export), variables and functions are scoped to the module by default. They are not available globally unless explicitly assigned to the global scope.

If you want the main function to be globally accessible, you need to assign it to the window object yourself:

// main.js
function main() {
    console.log('Hello, world!');
}

// Make main globally accessible
window.main = main;

This code ensures that main is available globally, allowing it to be called from anywhere in your application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.