Require() In Node.js

Require() is a Node.js function that is used to include the Node.js modules that are placed in separate files. If you want to get some basic information about Node.js modules, follow the link given below.

Let’s start with some easy examples.

Module.js

  1. /*Create Function On Fly*/  
  2. var Call=function() {  
  3. console.log("Hello! This is Call Function");  
  4. }  
  5. /*Function Take Name of Function as parameter */  
  6. var CallFun=function () {  
  7. console.log("Invoke call Function");  
  8.   
  9. }  
  10. /*Export callFun*/  
  11. module.exports.Method1 = CallFun;  
Module.js

App.js
  1. var obj=require('./Module.js');  
  2.   
  3. /*Invoke CallFun function Of main.js*/  
  4.   
  5. obj.Method1();  
  6. console.log("Hello! Nodejs");  
App.js

Output

Output

In this example, we create a Module.js file and define two functions in this file. At the end of file, we write the “module.exports.Method1 = CallFun;”. In this line of code, we use export method. Export is the object that's actually returned as the result of a require call. In an App.js file, we include the Module.js file, using require function and take the reference in Call obj. Using Call obj, we called the Method1.

Include multiple files

In previous examples, we exported the single module. What if we want to export multiple modules? Now, we are taking an example where we have two modules and we include both modules in App.js file. You can directly include both modules in App.js file, but best approach is to create another JavaScript file and include both modules in this file. At last, include this file into App.js file.

Module1.js
  1. /*Create call Function*/  
  2. var Call = function () {  
  3. console.log("Hello! This is Call Function");  
  4.   
  5. }  
  6.   
  7. /*Export Call Function*/  
  8. module.exports = Call;  
Module1.js

Module2.js
  1. /*Create CallFun*/  
  2. var CallFun = function () {  
  3. console.log("Invoke call Function");  
  4. }  
  5.   
  6. /*Export CallFun Function*/  
  7. module.exports = CallFun;  
 Module2.js

Export.js
  1. /* 
  2. include Both Module 
  3. */  
  4. var Call_ = require('./Module1.js');  
  5. var CallFun_ = require('./Module2.js');  
  6.   
  7. /* 
  8. Create Object that contain name of variable Call_, callFun_ 
  9. */  
  10. var Obj = {  
  11. Call: Call_,  
  12. CallFun: CallFun_  
  13. };  
  14.   
  15. /*Export Module*/  
  16. module.exports = Obj;  
Export.js

App.js
  1. var obj=require('./Export.js');  
  2.   
  3. /*Invoke CallFun function Of main.js*/  
  4.   
  5. obj.Call();  
  6. obj.CallFun();  
  7. console.log("Hello! Nodejs");  
App.js

Output

Output

Import JSON File

In previous examples, we exported and included only js files. Now, we take an example where we will include the JSON file in the code.

Employee.json
  1. {  
  2.     "Name""Pankaj Choudhary",  
  3.     "Age": 22,  
  4.     "City""Alwar",  
  5.     "State""Rajasthan"  
  6. }  
 Employee.json

Export.js
  1. /* 
  2. include JSON FILE 
  3. */  
  4. var Json = require('./Employee.json');  
  5. var Fun = function() {  
  6.     console.log("Employee Name= " + Json.Name);  
  7.     console.log("Employee Age= " + Json.Age);  
  8.     console.log("Employee City= " + Json.City);  
  9.     console.log("Employee State= " + Json.State);  
  10. }  
  11. module.exports = Fun;  
Export.js

App.js
  1. var obj=require('./Export.js');  
  2.   
  3. /*Invoke CallFun function Of main.js*/  
  4.   
  5. console.log(obj());  
  6. console.log("Hello! Nodejs");  
App.js

Output

Output

In this example, we create an Employee.json file, insert some data into it, and include this file in Export.js file. It is quite interesting that we don’t need to export the JSON file. At last, we include the Export.js file in App.js file and read all the data of Employee.json file.

I hope you liked this article. If you have any query or doubt, you can post it in the comment section. Thanks for reading the article.


Similar Articles