MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS Exporting and Consuming Modules

Creating a hello-world.js module


Node provides the module.exports interface to expose functions and variables to other files. The most simple way to do so is to export only one object (function or variable), as shown in the first example.

hello-world.js
module.exports = function(subject) {
 console.log('Hello ' + subject);
};

If we don't want the entire export to be a single object, we can export functions and variables as properties of the exports object. The three following examples all demonstrate this in slightly different ways :

.hello-venus.js : the function definition is done separately then added as a property of module.exports
.hello-jupiter.js : the functions definitions are directly put as the value of properties of module.exports
.hello-mars.js : the function definition is directly declared as a property of exports which is a short version of module.exports


hello-venus.js


function hello(subject) {
 console.log('Venus says Hello ' + subject);
}
module.exports = {
 hello: hello
};

hello-jupiter.js


module.exports = {
 hello: function(subject) {
 console.log('Jupiter says hello ' + subject);
 },
 bye: function(subject) {
 console.log('Jupiter says goodbye ' + subject);
 }
};

hello-mars.js


exports.hello = function(subject) {
 console.log('Mars says Hello ' + subject);
};

Loading module with directory name


We have a directory named hello which includes the following files:

index.js


// hello/index.js
module.exports = function(){
 console.log('Hej');
};

main.js


// hello/main.js
// We can include the other files we've defined by using the `require()` method
var hw = require('./hello-world.js'),
 hm = require('./hello-mars.js'),
 hv = require('./hello-venus.js'),
 hj = require('./hello-jupiter.js'),
 hu = require('./index.js');
// Because we assigned our function to the entire `module.exports` object, we
// can use it directly
hw('World!'); // outputs "Hello World!"
// In this case, we assigned our function to the `hello` property of exports, so we must
// use that here too
hm.hello('Solar System!'); // outputs "Mars says Hello Solar System!"
// The result of assigning module.exports at once is the same as in hello-world.js
hv.hello('Milky Way!'); // outputs "Venus says Hello Milky Way!"
hj.hello('Universe!'); // outputs "Jupiter says hello Universe!"
hj.bye('Universe!'); // outputs "Jupiter says goodbye Universe!"
hu(); //output 'hej'


Conclusion

In this page (written and validated by ) you learned about NodeJS Exporting and Consuming Modules . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Exporting and Importing Module.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.