Difference Between CommonJs and ES6 Modules ?

    What is a module and Why We need It?

    A module is simply code that can be exported from one file to another.  Module helps us to split up code into different parts.  Since from the beginning of NodeJS, the CommonJs module system is the default system and later on new module system was added -ES Modules.

    Usually, we split code into different files so that code should be organized and want to reuse code in a structured way. While this is a abecedarian part in utmost programming languages, this wasn't the case in JavaScript. Everything we write in JavaScript is global by dereliction. This has n’t been a huge issue in the early onsets of the language. As soon as inventors began to write full-bloated operations in JavaScript, still, it got them into real trouble. 

     This is why the NodeJS generators originally decided to include a dereliction module system, which is CommonJS. 

    CommonJS : Default Nodejs Module System

    In Nodejs all .JS files are handled as separate  CommonJS module which means the functions, variables, classes are not accessible by other files by default. So we need to tell the module which part of code should be exported. 

    This is done via “module.exports” object or the exports shortcut, which are both available in CommonJS module and When we want to import code we use “require()” function.


        
    //index.js
    
    //importing inbuilt modules
    //const package = require('module-name')
    
    const data=require('./module')
    
    console.log(data.msg);
    
    data.fxnMessage()
    
        
      
    Custom Module
        
        //module.js
    const msg="Hello from Module.js"
    
    function fxnMessage(){
        console.log("Hello");
    }
    
    module.exports={msg,fxnMessage}
        
      

    ES Modules : ECMAScript Standard

    Since the 2015 edition of the underpinning ECMAScript standard( ES2015) we actually have a standardized module system in the language itself, which is simply called ES Modules. 

     

     It took a while before the cybersurfer merchandisers and the NodeJS maintainers actually completely enforced the standard. This was eventually the case for NodeJS with interpretation 14, when it first got stable. So, let’s just dive into it.

     

    Instead of the require() function for importing modules, we now use a specific import syntax.

    Also, instead of a specific module object, we now use the export keyword in front of our class declaration. This tells the compiler, which parts of the file should be accessible by other files.


    
    //index.js
    
    //importing inbuilt modules
    //import package from 'module-name'
    
    import data from './module.js'
    
    console.log(data.msg);
    
    data.fxnMessage()
    
    
    
    Custom ES Module
        
    //module.js
    const msg="Hello from ES Module.js"
    
    function fxnMessage(){
        console.log("Hello");
    }
    
    export default {msg, fxnMessage}
        
      

    0 Comments