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
-
- var Call=function() {
- console.log("Hello! This is Call Function");
- }
-
- var CallFun=function () {
- console.log("Invoke call Function");
-
- }
-
- module.exports.Method1 = CallFun;
App.js
- var obj=require('./Module.js');
-
-
-
- obj.Method1();
- console.log("Hello! Nodejs");
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
-
- var Call = function () {
- console.log("Hello! This is Call Function");
-
- }
-
-
- module.exports = Call;
Module2.js
-
- var CallFun = function () {
- console.log("Invoke call Function");
- }
-
-
- module.exports = CallFun;
Export.js
-
-
-
- var Call_ = require('./Module1.js');
- var CallFun_ = require('./Module2.js');
-
-
-
-
- var Obj = {
- Call: Call_,
- CallFun: CallFun_
- };
-
-
- module.exports = Obj;
App.js
- var obj=require('./Export.js');
-
-
-
- obj.Call();
- obj.CallFun();
- console.log("Hello! Nodejs");
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
- {
- "Name": "Pankaj Choudhary",
- "Age": 22,
- "City": "Alwar",
- "State": "Rajasthan"
- }
Export.js
-
-
-
- var Json = require('./Employee.json');
- var Fun = function() {
- console.log("Employee Name= " + Json.Name);
- console.log("Employee Age= " + Json.Age);
- console.log("Employee City= " + Json.City);
- console.log("Employee State= " + Json.State);
- }
- module.exports = Fun;
App.js
- var obj=require('./Export.js');
-
-
-
- console.log(obj());
- console.log("Hello! Nodejs");
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.