Node.js Lesson 2: Modules and Exports

Node.js Lesson 2: Modules and Exports

Node.js Lesson 2: Modules and Exports

Hey everyone, we learned what are modules and how to create one in the previous lesson. This lesson will dive deeper with the module, understand what properties it carries, and then talk about export in detail. Let’s start.

Properties of Module

Every javascript file in the Nodejs project is a separate module and it contains a global module object. Let’s try to log in and see what’s inside. Go to index.js and add this code:

This will log the information about the module. Let us understand it’s properties briefly:

  • filename – full name of the file
  • id – generally contains a detailed path to the file. If an operation/file system supports symbol links, it means all symbol links will be drawn.
  • exports – all information that goes externally. We talked about it at our last lesson.
  • parent – a link to a parent module, i.e. the module that required this one.
  • loaded – shows whether a module has been uploaded. When the module gets moved to a console, it hasn’t been processed yet.
  • children – the modules that were connected by this module via require.
  • path – a sort of an inner ID.

Using these properties, the module keeps track of itself. These all properties have their relevant importance but right now we are interested in two parameters: parent and exports. Let us talk about these.

Parent Property and How to use it

A file in Nodejs can be run directly by calling node filename.js or it can just work as a module that exports a certain function to another.

We can use module.parent to check in which mode our file is running. module.parent === true means that a parent is calling this file using require(‘./filename.js) and if it is false then it is run directly by node filename.js

Using this check, we can tell the program to do two different things depending on how the code is executed. In the example below, we will export the function run() if it is working as a module and being called in a parent file. Otherwise, we will simply run the function.

If there isn’t a parent module, we can run this file independently using this feature. Now let’s talk about exports.

Exports and How to use it

When you’re building a Nodejs application, you will have to create a lot of modules that will become building blocks of your application. You need to understand how to export them and use them wherever required.

module.exports is an object that the current module returns when it is “required” in another program or module. Whenever we want something like values or functions to be available for another module to import and use, we attach that value or function with this module.exports


Export a few properties from one module:

Require and use it in another module:

We can use variables and methods of one module into another in a similar fashion.

We can also use exports instead of module.exports and achieve the same functionality. You can consider this as a shorthand of the longer method. Example, this will totally work fine:

Whereas, if you assign the properties directly to exports, it won’t’s work:

This happens because exports are an object and we assign properties to it. These properties then get exported to another module. By assigning directly to exports itself, we reinitialized that property which will break the desired functionality.

exports can be considered as an alias of module.exports to write less code. Imagine it as something like this:

Note: Please note that we are actually returning module.exports not exports.

Our next stage will be to add a database to the project.

Connect to Database

Let’s create a db directory and move our JSON file into it. Add a file index.js which will be a helper module to connect with db (en.json) and get phrases from the JSON file.

We just created a db module that has a connect() method to connect with the database and getPhrase method to fetch data. This method will return a respective phrase, but if not – it will show an error.

Now, let’s use this module in user/index.js

We just replaced requiring en.json with db.js and then logging phrase using db.getPhrase method.

This won’t work unless we connect with the database first. Go to index.js:

The first time a module is executed, it gets initialized. This is when we call connect() to connect to the database (JSON file in our case). After that, we can use anything from the database as we want.

We are using index.js to connect to our database. This looks like a task of a server. Let’s move the whole logic inside a file server.js and then import it in the root index.js as follows:

We have successfully connected with the database using Module ✨. Now, let’s talk about how modules are searched and what it has to do with Module Caching.

Module Caching

All the modules get instantiated with some null properties when required. Ours connect function is instantiating phrases with en.json instead of null.

What do you think will happen if we require db again?

You might think it will initialize the object again but that is not what happens. Requiring the same module in a different location doesn’t create different instances but use the one that was initialized the first time. This is what we call Module Caching.

The module gets cached during the first require statement, meaning only one instance of that object is created — subsequent require statements in other modules just enable us to access that same object.

It means that the changes with the db object will persist upon us trying to import db inside the server and then in the user module. We are using the same db object in both server.js and then in user/index.js

What we learned

In this lesson, we learned

  1. About the properties of the Module object
  2. What is Parent property and how to use it
  3. What is Exports
  4. How to export functionalities and use it in another module
  5. How to connect to the database (JSON file)
  6. What is Module Caching

That’s it for now, we will talk more about modules as we progress with the development. Our next lesson will be about the Node Package Manager (NPM).

Check out the source code of this lesson in this GitHub repo.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles


Nest.js and AWS Lambda for Serverless Microservices

By combining Nest.js and AWS Lambda, developers can harness the benefits of serverless computing to build flexible, resilient, and highly scalable ...


Interoperability between Ethereum, Binance Smart Chain, and other blockchain platforms using Node.js

In this article, I will deeply into the importance of interoperability in the blockchain sphere and present use cases that support this perspective. ...


Anime.js to MP4 and GIF with Node.js and FFMPEG

While canvas animation is often considered the simpler approach for exporting animations to video and GIF formats, it can also limit the range of ...

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy