Skip to content

Modules

Alongside Node.js, you have also installed npm, or the node package manager. With this, you can install libraries, or modules, similar to how you can use apt-get to install packages on a Linux-based OS.

What are modules ?

Good authors divide their books into chapters and sections; good programmers divide their programs into modules.

Like a book chapter, modules are just clusters of words (or code, as the case may be).

Good modules, however, are highly self-contained with distinct functionality, allowing them to be shuffled, removed, or added as necessary, without disrupting the system as a whole.

Creating a module

A Node module is a collection of files that export a function, object, value, or anything else. A simple module looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/**
 * simple.js
 *
 * Simple Node.js module
 *
 * Exports add and subtract functions
 */

function add (a,b) {
  return a + b
}

function subtract (a, b) {
  return a - b
}

module.exports = {
  add: add,
  subtract: subtract
}

The module.exports keyword is an object that contains the things that we will make available when another JavaScript file uses our module.

Using a module

Using a module in Node.js is done using the require() function. This function takes as argument the path to the JavaScript file that we want to use. For example, if we had the simple.js file in the same folder as the next file, here's how we would use the file:

1
2
3
4
5
6
7
8
9
/**
 * index.js
 *
 * Simple require example
 */

const simpleMath = require('./simple') // note how we don't write the .js extension

console.log(simpleMath.add(2,3)) // logs 5

Installing other modules

We can also use other modules. We can install modules from the npm repository. Before we can install anything, we must run npm init in the folder that we have our files. This will ask us a few questions:

node

Type a project name and accept the defaults for the rest. If we open up the package.json file, we can see something similar to this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "name": "test-project",
  "version": "1.0.0",
  "description": "A simple test project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Bogdan",
  "license": "ISC"
}

Don't worry about the test error on line 7, we'll talk about it later.

Great! Now we are ready to install packages. Let's install fetch, the network requests package that we used yesterday in the browser:

1
npm install --save fetch

This command will do two things:

  • it will create a node_modules folder that will contain the fetch module
  • it will add the fetch package to our package.json file, so that we can always install our required packages.

If we open up our package.json file again, we'll see that the fetch package has been added into a new section called dependencies:

1
2
3
4
5
6
7
{
  "name": "test-project",
  ...
  "dependencies": {
    "fetch": "^1.1.0"
  }
}

We can now use the fetch module in our files like this:

1
2
3
4
5
6
const fetch = require('fetch')

fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(e => console.log('Uh oh! An error occured'))

Notice that, apart from the first line, the code is exactly the same as what we did in the browser. Ain't that cool ?