Mastering Local Modules in Node.js

KolaKachi
This entry is part 9 of 35 in the series Node.js Unleashed: A Comprehensive Guide for Developers

Welcome back! In this post, we’ll embark on our exploration of local modules in Node.js. Local modules are the modules that we create ourselves, tailored to our application’s needs. They bring structure, reusability, and maintainability to our codebase.

Building on Our Foundation

We currently have an index.js file with a simple logging statement – “Hello from index.js.” Now, let’s enhance our script. In the same file, I’m going to introduce a function that calculates the sum of two numbers.

const add = (a, b) => a + b;

const sum = add(15, 25);
console.log(sum);

Executing node index.js should now log both “Hello from kolakachi.com” and the sum, which is 40. While this works perfectly, it’s a good practice to organize our code as the application grows.

Enter Local Modules

To achieve better code organization, we’ll create a separate module for the add function. In our Node.js folder, let’s craft a new file named add.js. This file represents our first local module.

// add.js
const add = (a, b) => a + b;

const sum = add(1, 2);
console.log(sum);

Now, we’ve isolated the add function into its own module. However, if you run node index.js now, you’ll notice that the log statement from add.js is not displayed. Why? Because in Node.js, each file is a module and is isolated by default.

Introducing require in CommonJS

To bridge the gap between modules, Node.js uses the CommonJS module format. We employ the require function to import modules into other files. In our index.js, we add:

// index.js
require('./add');

console.log("Hello from kolakachi.com");

Here, require('./add') loads the add.js module into index.js. When you run node index.js, you’ll observe both “Hello from kolakachi.com” and the sum, indicating that the add.js module was successfully executed.

Understanding the Flow

The require function loads the specified module into the current script. The code in the module gets executed, and control returns to the main script once execution is complete.

To highlight this, we ran our script in debug mode. As we stepped through the code, you could see how the control flowed between index.js and add.js. Understanding this flow is crucial for effective module usage.

Extensionless Modules

A neat trick in Node.js is that you don’t have to specify .js when requiring JavaScript files. Node.js automatically appends .js. Thus, running node index is equivalent to node index.js.

Looking Ahead

While we can successfully load and execute a module, it would be even better if we could expose specific functionality while keeping the rest private. This approach ensures that only necessary code is shared between modules. In the next post, we’ll explore how to achieve this, enhancing the power of local modules in Node.js. Stay tuned for more insights into mastering Node.js modules!

Series Navigation<< Unveiling the World of Node.js ModulesUnveiling the Power of Module Exports in Node.js >>

Leave a Reply

Your email address will not be published. Required fields are marked *