Monday, January 18, 2021

Module exports in Node JS

A module is a discrete program, contained in a single file in Node.js. Modules are therefore tied to files, with one module per file. Modules are available in other programming languages. Node.JS uses the CommonJS system of modules, but there are other module types used in the JavaScript ecosystem. The most prominent of these other module systems are the Asynchronous Module Definition (AMD) and the (ECMAScript 6) ES6 module systems.


As we will see, module.exports is an object that the current module returns when it is "required" in another program or module.


You can include functionality from other modules in any other module. To do so is referred to as "requiring" the module, which is simply calling for a certain special object representing the functionality of the module.


For everyday use, modules allow us to compose bigger programs out of smaller pieces. Modules become the basic building blocks of the larger piece of software that collectively, they define.



Under the covers, the module keeps track of itself through an object named module. Inside each module, therefore, 'module' refers to the object representing the current module. This object holds metadata about the module, such as the filename of the module, as well as the module's id.



Here is a little snippet of code you can run to see the values of these example properties on a module:


console.log(module.filename);

console.log(module.id);

console.log(module.exports);


In Node.js, the practice of making a module's code available for other modules to use is called "exporting" values.


Therefore, we can export any value or function or other object we would like to export by attaching it as a property of the module.exports object. For example, if we would like to export a variable named temperature, we could make it available for use outside the module by simply adding it as a new property of module.exports as follows:


module.exports.temperature = temperature; 


In order to import the module, we need to use a special keyword used to import things, and it is called require. Where module.exports lets us set things for export, require lets us specify modules to be imported into the current module.


The functionality for importing modules is provided in a module named require, available on the global scope. This module's main export is a function to which we pass the path of the module we would like to import. For instance, to import a module defined in music.js, we would require('./music'), where we have specified the relative path.



Exporting and requiring classes with module.exports 


In addition to functions and variables, we can also use module.exports to export other complex objects, such as classes.


function Cat(name) {

    this.age = 0;

    this.name = name;

}

 

// now we export the class, so other modules can create Cat objects

module.exports = {

    Cat: Cat

}


Now to use it, it is like below 


let cat = require(‘./cat’)

let Cat = cat.Cat


Let mummyCat = new Cat(‘Mummy’) 



References:

https://stackabuse.com/how-to-use-module-exports-in-node-js/



How to Create a node JS module 




References:

https://www.digitalocean.com/community/tutorials/how-to-create-a-node-js-module

No comments:

Post a Comment