Modules

Modules are libraries of Node.js. Each single file js file or folder in node js is considered as a module.

Like in frontend, we include Jquery library and then call jQuery functions, same in Node JS, libraries are included using module and then called. Modules are reusable in nodejs app.

Node.js supports two types of modules, CJS Modules and ESM Modules.

Modules Example

         /* ext.js */
   
    const pi=Math.PI;
    let r=5;
    const a=pi*r*r;
    
    exports.area=a;
         /* test.js */
   
    const cir=require('./ext');
    console.log(`Area of circle is ${cir.area}`);

Area of circle is 78.53981633974483

npm start

In the above example, ext.js is a module which exports area of a circle with radius 5. In our test.js file, we are including module using require function.


module.exports

module.exports or exports object is used to export data (string, number, boolean, function, array or object). module.exports expose object it point to, and exports expose the property it point to. There are two types of modules in node js, build-in modules and custom modules.

Build-in modules

Node JS build-in modules includes http module, os modules, path module etc.

To include build-in modules, require function is used.

    const http=require('http');
    const os=require('os');
    const path=require('path');

Custom modules

Custom Modules are external .js files with exports property. Same require function is used to include custom js modules.

export using module.exports for single export

         /* ext.js */
   
    const pi=Math.PI;
    let r=5;
    module.exports=pi*r*r;;

export using exports.property for multiple exports

         /* ext.js */
       
    const pi=Math.PI;
    let r=5;

    exports.area=pi*r*r;;
    exports.circumference=2*pi*r;
    

require

require function is used to include external modules in our app. For example, lets says our main file is test.js and module app.js exports a function or any other data type using exports object. See example

Import Module in Node JS

         /* ext.js */
   
    const pi=Math.PI;
    const r=5;
    exports.area=pi*r*r;;
    exports.circumference=2*pi*r;
         /* app.js */
   
    const cir=require('./ext');
    console.log(`Area of circle is ${cir.area}, and circumference is ${cir.circumference}`);

Area of circle is 78.53981633974483 and circumference is 31.41592653589793

npm start

As .js extension is common in both frontend and node, we prefer .js extension for frontend files only. For node application, avoid using .js extension to avoid confusion.


ES6 Modules in NodeJS

Node JS Also supports ES6 Modules since Node JS v12. require() is Common JS Module function used in Node as JavaScript lacks Modules earlier. So now we can also use ES6 Modules in Node JS.

CJS modules are synchronous, while ESM modules are asynchronous.

To Use ES6 Modules in Node JS, set a property "type" with value "module" in package.json file. i.e "type": "module".

Export for single export

         /* ext.js */
       
    const pi=Math.PI;
    const rad=5;
    const area=pi*rad*rad;
    export { area as a };
    export { rad as r };
         /* app.js */
       
    import {r,a} from './ext.js';
    console.log(`Area of circle with radius ${r} is ${a}`);
         /* package.json */
{
    "name": "nodeapp",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start":"node src/app.js" 
    },
    "type":"module",
    "author": "Avinash Malhotra",
    "license": "ISC"
}           

Area of circle is 78.53981633974483

npm start

export default

export default is used to export single value.

         /* ext.js */
       
    const pi=Math.PI;
    const rad=5;
    const area=pi*rad*rad;
    export default area;
   
         /* app.js */
       
    import area from './ext.js';
    console.log(`Area of circle is ${a}`);

import build in modules

to import build-in modules, like os, fs, use import os from "node:os". This will import os module for node js. This syntax also distinguish core node js modules from npm modules.

  
    import os from "node:os";

    console.log(os.cpus().length);