Home >> Tutorials >> Node.js Local Modules

Local Modules: (Custom or User-Defined Modules)

  • It's user defined node.js module.
  • Local modules are mainly used for specific projects and locally available in separate files or folders within project folders.
Load and Use Local Module with Example:

First you need to import core module using require() function.

Create a folder common, inside this common folder create a new file named utility.js with following code

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

Now, inside app.js or index.js file import that utility module using require() function and use.

//load/import module and use
const logger = require('./common/utility');   
var output = logger.log("Congrats!, You have successfully created a local module!");  
console.log(output); 
Full Stack Tutorials

Author @FullStackTutorials | View all Articles