ES2015 import doesn't work (even at top-level) in Firefox
Actually the error you got was because you need to explicitly state that you're loading a module - only then the use of modules is allowed:
<script src="t1.js" type="module"></script>
I found it in this document about using ES6 import in browser. Recommended reading.
Fully supported in those browser versions (and later; full list on caniuse.com):
- Firefox 60
- Chrome (desktop) 65
- Chrome (android) 66
- Safari 1.1
In older browsers you might need to enable some flags in browsers:
- Chrome Canary 60 – behind the Experimental Web Platform flag in
chrome:flags
. - Firefox 54 –
dom.moduleScripts.enabled
setting inabout:config
. - Edge 15 – behind the Experimental JavaScript Features setting in
about:flags
.
How can I fix 'import declarations may only be on top level` ECMAScript import errors
To make it work all you need to do is to mark both of these JS files as module in your index.html
file and it will work fine.
<html>
<body>
<script>
function f() {
alert("IT WORKS")
}
</script>
<script src="f.js" type="module"></script>
<script src="j.js" type="module"></script>
</body>
ES2015 import not working in node v6.0.0 with with --harmony_modules option
They're just not implemented yet.
Node 6.0.0 uses a version of V8 with most of ES6 features completed. Unfortunately modules isn't one of those completed features.
node --v8-options | grep harmony
in progress harmony flags are not fully implemented and usually are not working:
--es_staging (enable test-worthy harmony features (for internal use only))
--harmony (enable all completed harmony features)
--harmony_shipping (enable all shipped harmony features)
--harmony_object_observe (enable "harmony Object.observe" (in progress))
--harmony_modules (enable "harmony modules" (in progress))
--harmony_function_sent (enable "harmony function.sent" (in progress))
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress))
--harmony_simd (enable "harmony simd" (in progress))
--harmony_do_expressions (enable "harmony do-expressions" (in progress))
--harmony_iterator_close (enable "harmony iterator finalization" (in progress))
--harmony_tailcalls (enable "harmony tail calls" (in progress))
--harmony_object_values_entries (enable "harmony Object.values / Object.entries" (in progress))
--harmony_object_own_property_descriptors (enable "harmony Object.getOwnPropertyDescriptors()" (in progress))
--harmony_regexp_property (enable "harmony unicode regexp property classes" (in progress))
--harmony_function_name (enable "harmony Function name inference")
--harmony_regexp_lookbehind (enable "harmony regexp lookbehind")
--harmony_species (enable "harmony Symbol.species")
--harmony_instanceof (enable "harmony instanceof support")
--harmony_default_parameters (enable "harmony default parameters")
--harmony_destructuring_assignment (enable "harmony destructuring assignment")
--harmony_destructuring_bind (enable "harmony destructuring bind")
--harmony_tostring (enable "harmony toString")
--harmony_regexps (enable "harmony regular expression extensions")
--harmony_unicode_regexps (enable "harmony unicode regexps")
--harmony_sloppy (enable "harmony features in sloppy mode")
--harmony_sloppy_let (enable "harmony let in sloppy mode")
--harmony_sloppy_function (enable "harmony sloppy function block scoping")
--harmony_proxies (enable "harmony proxies")
--harmony_reflect (enable "harmony Reflect API")
--harmony_regexp_subclass (enable "harmony regexp subclassing")
Related Topics
Why Does My Http://Localhost Cors Origin Not Work
How to Create a ≪Style≫ Tag With JavaScript
Using Jquery to Center a Div on the Screen
When Is a Cdata Section Necessary Within a Script Tag
Get Element from Within an Iframe
Onclick to Get the Id of the Clicked Button
Load and Execution Sequence of a Web Page
Using an HTML Button to Call a JavaScript Function
Why Js Function Name Conflicts With Element Id
Full-Screen Iframe With a Height of 100%
Adjust Width and Height of Iframe to Fit With Content in It
Height Equal to Dynamic Width (Css Fluid Layout)
In What Situations Would Ajax Long/Short Polling Be Preferred Over Html5 Websockets
How to Check File Mime Type With JavaScript Before Upload
Creating a Div Element in Jquery
Determine Timezone from Latitude/Longitude Without Using Web Services Like Geonames.Org