- Getting Started with Node.js: An Introduction for Beginners
- Demystifying ECMAScript: Unveiling the Roots of JavaScript
- Unraveling the Mysteries of Chrome’s V8 Engine
- Unraveling the Dynamics of JavaScript Runtime
- Unveiling the Essence of Node.js: More than Just Code
- Getting Started with Node.js: Your First Steps in the World of JavaScript Beyond Browsers
- Navigating the Differences: Browser JavaScript vs Node.js
- Unveiling the World of Node.js Modules
- Mastering Local Modules in Node.js
- Unveiling the Power of Module Exports in Node.js
- Navigating Module Scope in Node.js
- Unveiling the Node.js Module Wrapper
- Decoding Node.js Module Caching: Unraveling the Wrapper
- Navigating Node.js Module Interactions: Unveiling Import-Export Patterns
- Demystifying module.exports vs. exports in Node.js Modules
- Mastering Node.js: Importing JSON and Watch Mode Unveiled
- Exploring the Core: A Dive into Node.js Built-in Modules
- Mastering Paths in Node.js: A Guide to the Path Module
- A Deep Dive into the Events Module
- Elevating Node.js Development: Extending EventEmitter
- Decoding the Digital Tapestry: Unraveling Character Sets and Encoding in Node.js
- Mastering the Art of File Handling with Node.js FS Module
- Unleashing the Power of Promises: Exploring Node.js FS Promises Module
- Unveiling the Power of Streams in Node.js: A Deep Dive
- Mastering Stream Efficiency with Pipes in Node.js
- Unveiling the Power of Node.js HTTP Module
- Mastering Node.js: Crafting Your First Server
- Crafting Dynamic Responses: Serving JSON with Node.js
- Elevating Your Node.js Server: Unleashing the Power of HTML Responses
- Unlocking Dynamism: Mastering HTML Templates in Node.js
- Mastering Navigation: A Guide to HTTP Routing in Node.js
- Elevating Node.js: The Power of Web Frameworks
- Demystifying libuv: The Powerhouse Behind Node.js Asynchrony
- Demystifying npm in Node.js: Unleashing the Power of Packages
- Decoding package.json in Node.js: Unveiling the Blueprint of Projects
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!