Modules
Learn how Node.js treats each file as a module to organize code efficiently. Understand how to import modules using require and export functions or variables with exports and module.exports. Discover the role of the module wrapper for scope management and practice creating your own module to solidify these concepts.
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.
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 ...