Trying to Understand How Node Loads Modules

Hi there. As you know we’re working quarantine mode for a while. I bought an e-book on Amazon.

Professional Node.js: Building Javascript Based Scalable Software

I don’t know is this book has its second edition. If so, please let me know. Anyway, I was reading a section called Understanding How Node Loads Modules. I already knew this section. Let’s dig into this section.

Module Accessing

The first thing I learned, modules are accessible by file path or file name. What does it mean? For instance, we have a module called axios that helping http requests. This module referenced by its name and it will map into a file path unless this module is a core module.

What is This Core Module?

Core modules are developed by Node.js core team and they expose some APIs for the other developers who use Node.js to develop software. For instance, process is a core module unless you specify a file path.

Third-Party Modules

Third-Party modules are developed by programmers just like you. A third-party module can be developed by your colleague, your best friend, your father, etc.

Each third-party module can only be on your computer or package distributor websites. You can install these modules using NPM or yarn. These are package managers.

Eventually, each module works with the same logic. Core modules expose APIs and third-party modules too.

Import Your First Module

For instance, we have a module called sum. We will use require function to call this module.

const sum = require('sum')

This function imports public APIs from a core module or a local module. What is this public API?

A public API can be anything. A class, a function or a variable can be an API. To make accessible a function, we have to export this function.

Export Your First Module

Our first module will responsible for the sum of two numbers.

function sum(a, b) {
    return parseFloat(a) + parseFloat(b)
}

But this module currently isn’t accessible by the other modules. Because we didn’t export it. To export this module, we’ll use module.exports. There are different ways to export a module.

The First Way

module.exports = sum

In this way, we export our function and it is accessible now. It can accessible like that;

const sum = require('./sum')

sum(3, 5)

The Second Way

In this way, we export an object, not a function. But this object contains our function. We can export multiple functions by this way.

module.exports = {
    sum
}

We can access this function like that;

1-)

const { sum } = require('./sum')

sum(3, 5)

2-)

const myModule = require('./sum')

myModule.sum(3, 5)

The Last Way

In this way, we use default. But this object contains our function.

function sum(a, b) {
    return parseFloat(a) + parseFloat(b)
}

function print(str) {
    console.log(str)
}

module.exports = {
    print
}

module.exports.default = sum;

We can import our module like that;

const sum = require('./sum').default

console.log(sum(3, 4))

Or we can call it like that;

const { default: sum } = require('./sum')

console.log(sum(3, 4))

We didn’t import print function. We have good architecture just now. Our modules reusable and accessible from everywhere. We can only import the modules we want.

Eventually, module.exports is an object. So you can set your keys with their values. For instance,

module.exports.sum = sum

These are basic information for the first part. In the next part, we will be digging into how Node.js loads modules.

Resources

As I mentioned in the introduction, I used a book called Professional Node.js: Building Javascript Based Scalable Software for this information.

Thanks for reading.