- 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 to our Node.js exploration! In this installment, we’re set to unravel some intriguing patterns you might encounter while importing and exporting modules in various projects. Each pattern is valid, and understanding them ensures you’re well-equipped for diverse coding scenarios.
Pattern One: Single Variable or Function Export
Let’s kick things off by creating a new file, math.js
. Inside, we define a simple addition function:
// math.js
function add(a, b) {
return a + b;
}
module.exports = add;
In our first pattern, we directly export a single variable or function. In index.js
, we import and use it:
// index.js
const add = require('./math');
console.log(add(2, 3)); // Output: 5
Pattern Two: Direct Assignment to module.exports
In the second pattern, instead of having module.exports
on a separate line, we assign the function directly:
// math.js updated
module.exports = function add(a, b) {
return a + b;
};
The import in index.js
remains the same, and the output should still be 5.
Pattern Three: Exporting Multiple Variables or Functions
Now, let’s explore exporting more than one variable or function. We modify math.js
:
// math.js updated
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { add, subtract };
In index.js
, we import and use both functions:
// index.js updated
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5
console.log(math.subtract(2, 3)); // Output: -1
Pattern Four: Direct Assignment Within module.exports
An alternative way to attach functions to module.exports
is by directly assigning them:
// math.js updated
module.exports.add = function(a, b) {
return a + b;
};
module.exports.subtract = function(a, b) {
return a - b;
};
This change does not affect the import or output in index.js
.
Pattern Five: Leveraging exports
Reference
In the fifth pattern, we revisit the function that wraps every module. Instead of module.exports
, we use the shorthand exports
:
// math.js updated
exports.add = function(a, b) {
return a + b;
};
exports.subtract = function(a, b) {
return a - b;
};
While this works and produces the same output, it’s generally advisable to stick with module.exports
for consistency and clarity.
Intrigued to know why? Stay tuned for the next video, where we’ll delve into the nuances of module.exports
versus exports
.
I hope these patterns provide you with a comprehensive view of module interactions in Node.js.