Creating modules in NodeJS

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

Here we write our own module that exports (which means expose) a function called “tip15” which will take a number like a dinner bill and apply a 15% tip to return the total amount to be paid including the tip.  Put the following code in calctip.js

exports.tip15 = function(amount) {
   return 1.15 * amount;
}

To use this exported tip15 function in your node files, put the highlighted code in app.js (or index.js) …

using exported function in node modules

using exported function in node modules

The skeleton code is based on the code from our first node example)

With caltip.js and app.js in the same directory, run “node app.js” and get result in your browser on port 3000 as …

result of module running

result of module running

Factory Model

However, a more object-oriented way of doing the same thing is to use the Factory model where the code for  calctip.js looks like this…

module using factacory model

module using factacory model

The function tip15 is now within a CalcTip object.  We hence all tip15() a method of the CalcTip object.

We export a function create_tipper that instantiates (creates) and returns an instance of the CalcTip object.  We can then call create_tipper in our main app.js file like this …

calling factory method

calling factory method

Notice that this time we pull the require function out to the top of the file, which is probably preferred.  Result when run would be as expected.

Constructor Model

The reason why using object-oriented method is useful is that we can easily add an additional method such as tip20 to our object like so…

export the object

export the object

Note that this time instead of exporting a function, we are using module.exports” to export the entire object.  See this line …

module.exports = CalcTip

This is yet another way of creating a module — the constructor model.

When we export the object like that, we use it in app.js like as shown below …

using the exported object

using the exported object

Another variant

Sometimes you might find code that uses a variant of the above like this …

subtle change in the export

subtle change in the export

Note the subtle change in the exports in calctip.js.  If we do it this way, we have to use it slightly differently in app.js …

using the module slightly differently

using the module slightly differently

Whichever method you use if of personal preference.  But you should at least be consistent on the usage throughout the web app.

Explanation of Node Modules

Think of every file in node is a module.  That means things in one file is not visible to another file unless they are exported.  Each module (or file) has “module” and “export” objects.  The “require” is a way to bring in stuff from one file to another.

For more complex modules, put module in a folder along with a package.json file that tells node the main js file to load.  If it can not find one, it will run index.js.

Also putting

“private”: true,

in package.json will tell npm to not publish it to its live directory.