Modules in Node.js



Node.js modules are set of functions that are used to include in your application.It is very similar to the libraries in javascript. When involving in development, we can either use built in modules or else we can create our own modules.

Usually Modules are not designed in the same file that we are developing in. Modules are created and stored in separate files for the easy use. So lets create a separate file to create a module.I have created a file called module.js for further proceedings

Now I am going to create a basic module to count the number of elements in an  array in my new file.
var value =function(array){
  return array.length + ' elements are there';
};
console.log(value(['A','B','C','D']));

Now run the command line and check the output






Now we have created our module in an separate file.Now we should make this function available outside the module.js file.For that type the following code in module.js file.
module.exports = value;

value is the variable name that I have used to assign the function.

 All we have to do is to include it to our relevant file . For this we use the require function which in on the global object in Node.js.Now lets got to our test.js file.

Here you have to type the following code init.
var value = require ('./module'); // same variable name has been called
console.log(value(['A','B','C','D']));

./module shows the file path that we have required the module. And make sure to remove the console command from the module.js  as it should consist only the function as we have already given the array data in test.js.

Now when we require the needed module, it will only show us the content of the variable which it exports. So according to our example the function stored in the value variable will be returned. 

Then run the test.js file and get the output.




Source code 

module.js


var value =function(array){

  return array.length + ' elements are there';
};
module.exports = value;

test.js

var value = require ('./module');
console.log(value(['A','B','C','D']));

Also you can call different functions in the same module file whenever you need. Lets see how to do that.

I have written another function in modules.js to get the sum of two numbers. Now your module.js file will looks like as follow.
//return array
var value =function(array){
  return array.length + ' elements are there';
};

//add two numbers
var sum=function(x,y){
  return `Sum of the two numbers are = ${x+y}`; // `` is used to make it easy in string concatenation
};
module.export= value;

Here we have only export the value variable.To export the the functions in the file we should change the code as follows.
module.exports.value = value; 

module.exports.sum = sum;


Moving on to the test.js file you have to change the require variable in to the name of module file name as bellow.
var module = require ('./module');

Also when you are calling the relevant function, you have to call it as follow
console.log(module.value(['A','B','C','D'])); 
console.log(module.sum(2,4));

Now run the command line and see the output.






Hope you got to know how to create a module and require it correctly.


Comments

Popular posts from this blog

Working with Buttons in Android Studio

Java Part 2 :How to Install Java

Ruby - Dynamic, Open source programming language