Cover image for Js Modules are Now supporting in Browsers

Js Modules are Now supporting in Browsers

saigowthamr profile image Sai gowtham ・1 min read

JavaScript Modules is now supporting in all major browsers.

Let's see how it works.

for demo purpose, I created an index.html and math .mjs, script.mjs.

you can create .js extension its, not a problem but use a .mjs extension for the consistency to know what are the modules and what are the scripts?.

math.mjs file

in above code, I created two functions and exported it.

Now, these functions are available to import.

script.mjs file

At final we are creating the HTML file with our modules.

we need to specify the type in script tag for the modules mime type is the module.so that browser treated it as a module rather than normal script.

in above code 15th line I used to tell the browser it is not a module and if the modules are not supported in browsers please use this fallback.js.

final output is

Hope you guys love these.


Editor guide
rhymes profile image

currently, Mozilla is not supporting modules.

Apparently since Firefox 60 you have modules: developer.mozilla.org/en-US/Firefo...

Maybe you can test it...