In this post, we learn about the Export module in Node JS. Whenever we do any sort of programming or any task for that matter, we may need to export our results in say another format or another file. To do this we have to take help of export functions. Thus, what is it that you are waiting for? Let’s get going with export node JS.

Export Node JS

In this section, you will learn how to output different types as modules using module.exports. Before that let’s see what is module.exports. The module.exports object is a special object that is included by default in every JavaScript file in the Node.js application. Module is a variable that represents the current module, and “exports” is an object that is represented as a module. So, whatever you assign to module.exports will also be displayed as a module.

Now let us look at how you can output different types as a module using module.exports.

Export Node JS

Exporting literals

As mentioned earlier, exports is an object. So, it represents everything you have assigned to it as a module. For example, if you assign a string literal, that string literal is represented as a module. The following example shows a simple string message as a module in New.js file.

 module.exports = 'Hello Shubham';

To import this file New.js and use it, we have to proceed as below in “app.js” file:

var msg= require('./New.js');
console.log(msg);

On running it, we get the output as:

C:\ > node app.js
Hello Shubham

Want to know about getting to work with MongoDb with Python? Read here to learn more !! ~~>

Exporting Objects

The export is an object. So, you can assign properties or methods to it. The following example exports an object with a string property in the New.js file.

exports.Message = 'Hey you !';

// you can also write 
module.exports.Message = 'Hey you !';

In the above example, we have attached a Message property to the exports object. Now import and use this module as below:

 var msg = require('./New.js');
console.log(msg.Message);

Thus, the require() function returns an object {Message : ‘Hey you! ‘} which it then assigns to the msg variable. Hence, we can now make use of msg.Message.

So, to see the output we run the node app.js command in the command prompt and get the result as:

C:\ > node app.js
Hey you !

Likewise as above, you can also provide an object with a function. The following example represents an object with the log function as a module.

module.exports.log = function (msg) { 
                  console.log(msg);
};

The above module exposes an object – { log : function(msg){ console.log(msg); } } . Use it as shown:

var msg = require('./Log.js');
msg.log('Hey you !');

When you run the above code, the output will be as:

C:\ > node app.js
Hey you !

In order to append an object you can do as given below in the sp.js file:

module.exports = { firstName: 'James', lastName: 'Bond'
}

Exporting a function

You can attach an anonymous function to an export object, as shown below.

module.exports = function (msg) { 
                 console.log(msg);
};

Now you can use the above module as shown below:

var msg = require('./Log.js');
msg.log('Hey you !');

The variable msg becomes a function expression in the above example.

Export Node JS

Exporting function as a Class

Let us see how to do it:

module.exports = function (first, last) { 
                 this.fName = first; 
                 this.lName = last; 
                 this.fullName = function () { 
                          return this.first + ' ' + this.last; 
    }
}

To show the module, let us see how to do it:

var one = require('./Person.js');
var one1 = new person('Shubham', 'Ji');
console.log(one1.fullName());

Now, when we run the above code, we will get an output as:

C:\ > node app.js
Shubham Ji

This way you can export and import a local module created in a separate file in the root folder. Node.js also allows you to create modules in subfolders.

WRAPPING UP !! ~~> ~~>

In this post, thus we have learned about the Export module in Node JS. Whenever we do any sort of programming or any task for that matter, we may need to export our results in say another format or another file. To do this we have to take help of export functions. Thus, I hope that this post solves your problem and you have success in exporting modules, classes and functions alike. On that note, until next time, see ya!! Goodbye !! ~~> ~~>

:: ~~> ~~> :: ** :::::: ** :: )) ** :: ** (( ~~> ~~>

Categorized in: