Creating, Exporting and Loading Modules in Node

In this post we will cover the basics of what Modules are, how to create modules, export modules and require (reference) a module. If you are already aware of what Modules are, you can skip the first part and jump straight away to the hands on examples.

What are Modules?

One of the disadvantages of JavaScript is the global namespace sharing amongst different scrips. This is caused by the fact that when you load one or more JavaScript files/code, it is injected to one global namespace and thus if you had defined one global variable on one file, and the same in the other file, this will confict and create all sorts of bugs. This is where node helps in fixing this issue. Node used CommonJS to standardize packaging of JavaScript Libraries, called modules. Each module has its own context and does not conflict or interfere with other modules which eliminates the use of one global namespace by exposing a public API that can be used any time after importation.

A Module can be classified either as a Core Module, File Module, Folder Module or a third-party Module.

  • Core Modules expose some Node core functions that a programmer needs which are preloaed when a Node process starts. When referencing a Core Module, only the module name is used.
  • A file or folder module, are modules created by the programmer himself or others around him and are referenced by the file path or by file name. When referencing a module by file name, it will automatically map it into a file path, taking the assumption it is in the current file path.
  • A folder module can be referenced by the folder path without the filename and node will assume this folder is a package. Thus, node will look for a package definition in json format with the name package.json and if not found it sets the default file value of index.js.
  • Third-party Modules are modules available to download and install. Installing third-party modules is made easy through the NPM (Node Package Manager) and are referenced by module name.

Creating and Exporting Modules

Creat a folder and name it testingModules, inside place a JavaScript file named myFirstModule.js and open in edit mode.

Write a function as needed, in this case i will create a simple Math function to demonstrate how Node Modules work:

function three_squared()
{
console.log(Math.round(3 * 3));
}

function five_squared()
{
console.log(Math.round(5 * 5));
}

module.exports.three_squared= three_squared;

module.exports.five_squared = five_squared;

Note: Look at the last two lines. There we are defining what is to be exported by the module. We can have one or more module exports, in face here we have two different exports. “module” is a variable that represents the current module the programmer is in, while “module.export” is the object that the module will export to other scripts who require this module.

Loading Modules

To reference a module, the require function is used as follows:

var myCoreModule = require('http');

var myFileModule = require('../testingModules/myModule');  //equivalent to var myFileModule = require('../testingModules/myModule.js');

var myFolderModule = require('../testingModules');

Lets create a JavaScript file named test.js to load the module we created above:

var myModule = require('./myFirstModule');
myModule.three_squared();
myModule.five_squared();

Running test.js on Node

Open the cmd (command-line):

  • change directory to the folder “testingModules” (to change directory type cd following the path)
  • type “node test.js” and enter.

This should give print 3 squared (9) on one line and 5 squared (25) on another line as shown below:

module

Here we learned how to create, export and also loading modules, hope all was clear 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s