A lawyers' guide to understanding "module.exports" and "require" in nodejs.

·

3 min read

Many times we in our day-to-day use of nodejs, we rely on requiring or importing some functionality to our current file. Requiring of a file and its importation is made possible with the "require()" function which reads, executes, and then returns the "export" object of the target file.

In the node js module system, each file we create is treated as a module. A module object is created for each file. This module object has properties of which "exports" is included. Before I show this, let's do a quick setup.

I will be using the Vscode IDE for the purposes of this demo. I created a folder in my desktop directory which I will add to my Vscode workspace. The setup basically requires us to have a folder and two files in it initially. I call the files file1.js and file2.js.

Screenshot (25).png

Screenshot (26).png

Screenshot (27).png

Require

"...the "require()" function which reads, executes, and then returns the "export" object of the target file."

I had stated earlier that each time we require a module node reads, executes, and then returns the export property of that file's module object. To see the read and execute part in action, see the sample I did below.

Screenshot (28).png

Screenshot (29).png

In file1 we required file2. In file2 we had a console.log statement. Now when we run file 1, we get to see the console.log in file 2 executed. This lends credence to the quote above that the file we require is read and executed by node.

I had said earlier that each file is a module object in node. To see this in action, I did the following below.

Screenshot (30).png

Screenshot (31).png

As shown above, when we logged out "module", we could see the module object was logged to the console and amongst its properties is the "export" property. The path property is an interesting property to talk about.

Screenshot (31).png

The path property tells us the look-up paths where nodejs searches when we require a module. If the module is not found in either of these paths, we get the Error: Cannot find module error message. In our current setup, we are providing our own custom paths for node to look up for our module. If we were to require('file2') without providing the relative paths as so "./file2", we are invariably telling node to use the paths in the path property to look for our file.

In our current case, we will get an error. Why? because node will look for the node_modules module in the paths to check if we have such a file. Since we don't, it throws an error. If we create a node_modules directory in our current working directory and add file2 there, we will not have an error.

Module.Exports

When we do module.exports, we are basically adding whatever we need to export to the exports property in the file's module object. This is what is accessed by the other file requiring our module. The image below shows what the module object looks like in file 2 when we did a module.exports.

Screenshot (32).png

This is what enables us to have access to whatever was exported from that file via the module.exports in the file making the request which in our case is file1.

I hope this was helpful. There are some more things I did not touch but I hope to do so in the next part of this article. Thanks for reading.