NA +91-8727899942 thecoderjob@gmail.com

CHAPTER - 8

Understanding modules and require in node Js

Modules are a way to organize code into reusable pieces.

Each file in Node.js is considered a module, and you can export functions, objects, or values from one module to be used in another module using the module.exports object. Conversely, you can import these exported values into another module using the require() function

Here's is the Code !!

//Creating a Module
// Exporting functions
exports.add = function(a, b) {
    return a + b;
};
exports.subtract = function(a, b) {
    return a - b;
};

// app.js
// Importing the math module
const math = require('./math');

// Using the exported functions
console.log(math.add(5, 3));      // Output: 8
console.log(math.subtract(5, 3)); // Output: 2

Chapter 7

Writing first Node Js program with express framework.

Previous chapter

Chapter 9

Some core modules example in node Js.

Next chapter

Get In Touch

NA

thecoderjob@gmail.com

+91-8727899942

Popular Links

© www.thecoderjob.com. All Rights Reserved.               Designed by HTML Codex