Modules
Learn how modules work in Node.js.
We'll cover the following
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.
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 ofmodule.exports
before the module is evaluated. We can think ofexports
as a shorthand way of writing code. Instead of writingmodule.exports.Something
, we can writeexports.Something
. We are using this in our square.js file. - The
module.exports
object is created by theModule
system. We can usually use theexports
shorthand without ever needing to usemodule.exports
. However, if we overrideexports
by assigning it to something, else such asexports = function(){}
, thenexports
would no longer point tomodule.exports
. This is because the reassignment breaks the reference betweenmodule.exports
andexports
. Sincemodule.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 tomodule.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!
// import your module here// set your exported function herevar square = null
Get hands-on with 1300+ tech skills courses.