Node Tuts

Detach script window ⇗

You can navigate the video and the script by using the ← and → cursor keys.

Modules

Modules are the way in Node that you can package code for reusability.

Node provides several core modules that we have used previously. You can also use the module system for creating and using your own local modules, and you can also install and use third-party modules.

Using require()

The require command is the only mechanism provided of getting a reference to a module. A typilcal use of require is:

var http = require('http');

Here we can see several characteristics of require:

First, it's a synchronous call. Require can do some I/O — like accessing the file system. This is designed like this so that you don't have to use nested callbacks to load all your dependencies.

This means that require is designed to be used before Node starts the event loop and should not be used inside callbacks, with the risk of blocking the event loop.

This is wrong:

require('http').createServer(function(req, res) {
  require('fs').createReadStream('/path/to/my/file')
    .pipe(res);
});

This is correct:

var fs = require('fs');
require('http').createServer(function(req, res) {
  fs.createReadStream('/path/to/my/file')
    .pipe(res);
});

Second, require does not change any global namespace. Instead, it returns the module object which you can assign to a local variable.

Requiring a Core Module

We've seen some examples where we are requiring a Node Core module:

var http = require('http');
var fs = require('fs');

A Node Core module is identified by a string. There are several Node core modules like http, fs, net, tls, path, util and others.

Creating a Local Module

You can use the Node module system to create your own module. In Node, a module is one file.Let's create a module that encapsulates some characteristics of a Circle.

function Circle(x, y, r) {

  function area() {
    return Math.PI * Math.pow(r, 2);
  }

  function circumference() {
    return Math.PI * 2 * r;
  }

  function translate(dx, dy) {
    return Circle(x + dx, y + dy, r);
  }

  function scale(dr) {
    return Circle(x, y, r * dr);
  }

  return {
      x: x
    , y: y
    , r: r
    , area: area
    , circumference: circumference
    , translate: translate
    , scale: scale
  };
}

module.exports = Circle;

Save this file into a file named "circle.js".

You can see here that we created a constructor function named "Circle" that creates a circle object with some properties. Then we are assigning that function to the module.exports, which is a way to express what we want the users of the current module to see. Whatever we place on the module.exports is what the users of our module will get when requiring it.

Requiring a Local Module

We can now use the module we have just authored. For this we place ourselves in the same directory of our module and open the Node REPL:

$ node
> _

Then we require our module:

var Circle = require('./circle.js');

First, notice that, unlike a core module, a local module is defined by the path to that module. In this case we're using a relative path, but the require function also accepts an absolute path.

One thing that we can omit is the file termination, like this:

var Circle = require('./circle');

If we place our circle module inside a lib folder:

$ mkdir lib
$ mv circle.js lib

We can now require our circle module by doing:

var Circle = require('./lib/circle');

The path is always relative to the directory of the current module we are in.

Now we can use our module:

> var Circle = require('./lib/circle');
[Function: Circle]
> var c = Circle(1, 2, 3)
undefined
> c
{ x: 1,
  y: 2,
  r: 3,
  area: [Function: area],
  circumference: [Function: circumference],
  translate: [Function: translate],
  scale: [Function: scale] }
> c.area()
28.274333882308138
> c.circumference()
18.84955592153876
> c.translate(10, 10)
{ x: 11,
  y: 12,
  r: 3,
  area: [Function: area],
  circumference: [Function: circumference],
  translate: [Function: translate],
  scale: [Function: scale] }
> c2 = c.translate(10, 10)
{ x: 11,
  y: 12,
  r: 3,
  area: [Function: area],
  circumference: [Function: circumference],
  translate: [Function: translate],
  scale: [Function: scale] }
> c3 = c2.scale(2)
{ x: 11,
  y: 12,
  r: 6,
  area: [Function: area],
  circumference: [Function: circumference],
  translate: [Function: translate],
  scale: [Function: scale] }

Summary

We've here seen how you can use require to load core modules.

We've also seen how to create and use your own local module.

On the next episode we'll learn how you can install and use third-party modules.