Modules

Learn how modules work in Node.js.

What is a module?

Let’s explore what a module is in the context of Node.js.

Each separate file is treated as a module in Node.js. The most common use of modules is to use code or functions across different modules. This allows us to keep our code organized, as each module can serve a specific purpose. This may seem like a circular argument; let’s see what we mean by this in the code widget below. Be sure to go through all three files.

Press + to interact
index.js
square.js
shape.js
const square = require('./square.js')
const shape = require('./shape.js')
console.log("Area of the square is" , square.area(5))
console.log("Perimter of the square is" , square.perimeter(5))
const myShape = new shape("Hexagon", 6)
myShape.info()

require

require is used to import modules, JSON files, and local files. It is passed a relative path as a string. In our case, we have imported both of our modules on line 1 and line 2 of the index.js file.

The module wrapper

Before a module is exported, Node.js wraps the module in a wrapper function. This is how that looks:

(function(exports, require, module, __filename, __dirname) {
// Module code actually lives in here
});

This wrapper allows us to:

  • keep variables, defined in the module, scoped to the module rather than the global object
  • use variables, such as __dirname and __filename, for accessing the directory path and the filename of the module

While we may not be able to use this wrapper directly, it is important to know what Node.js is doing behind the screen, so that we can better understand the process.

exports or module.exports

All the variables or functions that we want to be accessible in other modules have to be exported. There are two ways of exporting them: using exports or module.exports. Let’s see how they differ from each other.

  • The exports variable is available within a module’s file-level scope. It is assigned the value of module.exports before the module is evaluated. We can think of exports as a shorthand way of writing code. Instead of writing module.exports.Something, we can write exports.Something. We are using this in our square.js file.
  • The module.exports object is created by the Module system. We can usually use the exports shorthand without ever needing to use module.exports. However, if we override exports by assigning it to something, else such as exports = function(){}, then exports would no longer point to module.exports. This is because the reassignment breaks the reference between module.exports and exports. Since module.exports is what really gets exported, exports will no longer work as expected. In our shape.js file, since we are only exporting one class, we can get away with assigning the class directly to module.exports.

Your own module

Create a module that takes a number and returns its square. Implement your function named square in the square.js module. Import it into index.js, and set your imported function in line 4. Hit the Test button when you are ready. There are hints to help you if you need it!

svg viewer
Press + to interact
index.js
square.js
// import your module here
// set your exported function here
var square = null

Get hands-on with 1300+ tech skills courses.