DEV Community

Cover image for Js Modules are Now supporting in Browsers
Sai gowtham
Sai gowtham

Posted on

Js Modules are Now supporting in Browsers

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.

Top comments (1)

Collapse
 
rhymes profile image
rhymes

currently, Mozilla is not supporting modules.

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

Maybe you can test it...