I require your assistance to help me understand how JS engines may work.
Context
Lately, I've been working with JS modules and I used the same pattern every time :
A specific_html_file.html :
<head>
<script type="module" src="javascript/import_yaml.js"></script>
</head>
A import_yaml.js :
import {DomFuncs} from './import_js/dom_functions.js';
window.addEventListener (`DOMContentLoaded`, DomFuncs.initImportPage);
A dom_functions.js :
import {SpecFuncs} from './specific_functions.js'; // Used in script
export class DomFuncs {
// Different DOM related functions
}
Question
This worked pretty well for the two first pages I wrote but the last logs an error in the console.
"Uncaught SyntaxError: Unexpected token {" on Chrome,
"SyntaxError: import declarations may only appear at top level of a module" on Firefox.
But in the end, all works perfectly fine so :
Why does the engine prints an error but works anyway in this particular file ? Why doesn't it react the same way in the first two pages ?
Thanks in advance,
A junior dev trying to understand as many things as possible