Local Modules: (Custom or User Defined Modules)

  • A node module which is developed for some specific purpose is called cutom or local or user defined module in node.js.
  • Local modules are mainly used for some specific purpose in projects and locally available in separate files or folders within project.
Let's see how to create or develop a local user defined (custom) module.
  1. Create and develop and export a custom module in your project locally.
  2. Import that module and use them throughout your project as per requirement.

1. Create a folder modules, inside your project and create a new file named utility.js within this modules folder with following code.

//create custom module and export
function log(message) {  
module.exports = log; 

2. Suppose you want to use it in your app.js file then first import that utility.js module using require() function (In Vanilla JS) OR import (In ES6) and then you can use this custom module in your app.js file.

//import custom module
const Utility = require('./modules/utility'); 

//use custom module
const logMessage = Utility.log("Congrats!, You have successfully created a custom local module!");