Modules in JavaScript confusion

I am simply trying to execute below javascript code ,involving the concept of module but getting unexpected error .

I am referring modules in javascript of code academy and in codecademy platform , code is running successfully .

// index.html

<!doctype html>

        <title>Page Title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="initial-scale=1.0">


<script src="abc.js"></script>
<script src="abcd.js"></script>


// abc.js

let Airplane = {};

Airplane.myAirplane = "StarJet";

module.exports = Airplane;

// abcd.js 

const Airplane = require('./abc.js');

function displayAirplane() {

Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown 路 Publish articles on custom domain 路 Gain readership on day zero 路 Automatic GitHub backup and more

Ibrahim Tanyalcin's photo

Thats not meant to be run on client side. For commonJS, check for instance rollup or Browserify.


But, I have thought about this bundler issue for an enterprise project that has to work ie10+. I wanted to support latest ES6 stuff like promises but also fallback to rAF if necessary without any transpilation + build time. You can run this script like this:

    var abc = function()
        let airPlane = {};
        airPlane.myAirPlane = "StarJet";
        taskq.export(airPlane,"airPlane");//export it airPlane with a alias
    abc._taskqId = "abc"; //give it an id
    taskq.push(abc);//push to queue, it will be automatically executed when conditions are right
    var abcd = function(airPlane){
        function displayAirplane(){
    abcd._taskqId = "abcd";
    abcd_taskqWaitFor = ["abc"]; /*an array of ids that this function should wait before it executes*/

then in your html:

<script src="abc.js" async></script>
<script src="abcd.js" async></script>

The above order of scripts does NOT matter, put them anywhere you like.


Check for taskq in npm or in my github. I have a new version which takes a bit longer to sort the dependency tree (~1ms, but more stable), I will upload it soon. You are also able to make dynamic imports that respects the queue order, which I will not explain here for clarity.

Matt Strom's photo

Unfortunately, modules (more precisely, CommonJS modules) are not natively available in the browser. You need to use another module systems such as RequireJS, SystemJS, Browserify, Webpack, or the Universal Module Definition (UMD) pattern to use modules in the browser.

Also unfortunately, those are not the easiest libraries to use.

Jason Knight's photo

The "not natively available in browsers" being just part of why I've had little reason to even touch them.

That and it seems like a crutch for people who can't organize their code properly.