LoadingDockEquipmentOne of the great benefits of utilizing a language like Node.js is the wide assortment of 3rd party modules that are at your disposal.  From calculating the validity of credit card numbers, to managing network connections, the Node Package Manager (npm) repository has hundreds of thousands of packages available to you.  This article will explain how you can load modules into your project.

Loading and Referencing a Module

In Node.js, modules are accessed either by their name or the file path on the system.  Non-system modules referenced by name will eventually be mapped into a file system path.

Each module exposes a public interface that can be used after the module is imported with the "require" into the current script.

var mod = require("some_module_name");

The previous command will import and return an object that represents the external API exposed by the module.  The value of that object is dependent on the module and can include an object, an array, a function, or any other type of JavaScript object.

Developing and Exporting Modules

For the module developer, you must use the CommonJS Module System to make your modules available to the outside world.  Let's create a sample module “person.js” that exports a Person constructor.

/* File person.js */
function Person(name) {
  var _name = name;
  function name() {
    return _name;
  }
}
module.exports = Person;

In the above example, the Person function is defined and then specified with the "module.exports" object that the module will export to other scripts that require this module.

As mentioned above, more complex objects can be exported by adding onto the module.exports object with whatever values you deem fit for your module.

/* file mymodule.js */
function sayHi() { console.log("HI!!!"); }
function sayBye() { console.log("BYE!!!"); }
module.exports.sayHi = sayHi;
module.exports.sayBye = sayBye;

Below is the code to load the module and call the sayHi and sayBye functions.

var mymodule = require("./mymodule");
mymodule.sayHi();
HI!!!
mymodule.sayBye();
BYE!!!

Loading a Module

There are a couple of ways to reference modules depending on their kind.  There are core modules, 3rd party modules, or local modules.

Core Modules

Since core modules are part of the distribution, you need only refer to it by it's name (not the path).  Core modules with name collisions to local modules take precedence.  The following line will load a instance of the core "http" module.

var http = require('http');

File Module

A file module is a one that is defined in a single file on the file system.  The person.js example above is an example of a file module.  When loading a file module, you supply a path (full or relative) to the module file, optionally omitting the .js extension.

var mymodule = require("/home/joe/node/modules/mymodule");
var mymodule = require("./mymodule");

In those examples the loader will look for a file named "mymodule.js" in the specified directory.

Folder Modules

Like a file module, when loading a folder module, you specify the absolute or relative location of the folder.  The Node loader will assume this folder is a package and attempt to look for a package definition file (package.json) in which the name and location of the module file are located.  If no package.json file exists, the Node loader will look for a file named index.js.

Loader search procedure

If the module name is not a path and is not a core module, the loader will attempt to find it inside the special "node_modules" folder.  If the module doesn't exist there, it will continue up the path, folder by folder, looking within node_modules folders on those directories until either finds the module or hits the root of the file system.

Conclusion

That's about all there is to it for exporting and loading a Node.js module.  Creating modules is as easy as writing the module code and specifying what is returned with the module.exports object.  Then a simple call to require("module") is all the client has to write to make use of it.