Tutorial: Creating Node.js modules

Node.js Modules

In this short tutorial I’m going to explain how you can create your own modules in Node.js, as everyone knows we cannot keep our entire code in only one js file, as your application grows it’ll be almost impossible for you to maintain your code, for this reason it’s essential that we separate our code in modules, this way it’ll hopefully always be organized and easy to maintain.

Node.js makes it very easy for us to create custom modules, let’s suppose we need to put 2 functions in a separated module, we just need to create a new js file like this:

newModule.js

1
2
3
4
5
6
7
exports.function1 = function(){
	console.log('Function 1');
}
 
exports.function2 = function(){
	console.log('Function 2');
}

As you can see it’s a very straightforward code, all I did was to create 2 functions and add them to module.exports, unless you don’t want your functions to be visible to other modules it’s very important that you add them to exports.

Now you can require this new module in another module as follows:

app.js

1
2
3
4
var newModule = require('./newModule');
 
newModule.function1();
newModule.function2();

When requiring a module you have to pass its full path to the require function, in this example I’m assuming that the 2 js files are in the same folder. After requiring your module you’re done! Now this app.js file has access to all the functions you’ve exported from your new module.

Recommended for you

Angular2 Tutorial: Developing a Realtime Chat App Angular 2 Chat Application Hi guys, today we're going to make an awesome chat application with Angular2, NodeJS and Socket.io, this tutorial will involve quite a lot of things, and there are also lots of small details, so it will be a little bit longer than usual. To easily understand and make t...
Nodejs Totorial: Creating and Using Events Nodejs makes it very easy for us to use events, and we're not limited to a collection of predefined events, it has a built-in module that allows us create and emit our own events. Let me show you how easy it is to use this module, first we have require the events module and create the event emitt...
How to Execute a .jar File with Node.js Child Processes You probably have already come across situations where your application had to execute other applications or native commands from the OS. Node.js can easily do that by spawning child processes. To show you how to do that I'm going to give you an example of how to execute a .jar file with node. Fi...
Node.js Tutorial: Reading and Writing files Reading and writing files is a very trivial task, no matter what programming language you use you probably have already came across a situation where you had to do it, and it's not different for node.js developers, luckily node.js makes this task very easy for us, with the file system(fs) module we ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Obs: Use the tag <pre lang="LANGUAGE"> to include code blocks to your comment.
Example: <pre lang="javascript"> console.log('Test'); </pre>