a blog for those who code

Saturday 12 July 2014

Creating Own Custom Module in Nodejs

In this post I will show you how you can create your own custom module in Nodejs. You might have been using Utilities, Colors etc modules in nodejs, like this you even create your own custom Modules.

To create our own custom module in Nodejs we have to do two things, i.e. its a two step process :
  1. Using exports object to export the function.
  2. We will create a variable of the full library treating it as single imported object, in order to access the functions.
Lets take an example.For First step :

exports.display = function(str) {

We need to export all the exposed functions using the exports object as shown above.For Second step :

var disp = require ('./myfunctions.js');
console.log(disp.display('My Function Display'));

At first we have to import it in current session using require and assigning the library to a variable name. Once assigned we can call any of the exposed functions in our code as shown above.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment