Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, and it uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js uses modules to organize and reuse code. A module is a discrete unit of functionality that can be shared and used in other programs. Modules in Node.js follow the CommonJS module system.
A module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node.js application. Each module in Node.js has its own context, so it cannot interfere with other modules or pollute the global scope. Additionally, each module can be placed in a separate .js file under a separate folder.
Core Modules: These are modules that come with Node.js itself when it is installed. Examples of core modules include the 'fs' (file system), 'http' (HTTP), and 'url' (URL) modules.
Local Modules: These are custom modules created locally in your Node.js application. These modules include different functionalities of your application in separate files and folders.
Third-party Modules: These are modules developed by other developers and can be reused in your application by installing them using npm (Node Package Manager). For example, express is a third-party module used to build web applications.
Here is an example using the 'fs' core module, which provides an API to interact with the file system.
const fs = require('fs'); // Importing the 'fs' module.
// Writing to a file.
fs.writeFile('example.txt', 'Hello, World!', (err) => {
if (err) throw err;
console.log('File has been saved!');
});
// Reading from a file.
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data); // Outputs: Hello, World!
});
Let's create a simple local module. Assume we have a file named mathOperations.js which contains a few mathematical operations.
// mathOperations.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;
const divide = (a, b) => a / b;
module.exports = { add, subtract, multiply, divide };
Now, you can use the `mathOperations` module in another file.
// app.js
const mathOperations = require('./mathOperations');
console.log(mathOperations.add(5, 3)); // Outputs: 8
console.log(mathOperations.subtract(5, 3)); // Outputs: 2
console.log(mathOperations.multiply(5, 3)); // Outputs: 15
console.log(mathOperations.divide(5, 3)); // Outputs: 1.6666666666666667
Here’s an example using the ‘express’ third-party module. You can install it using npm.
ell
npm install express
Now, you can use the 'express' module to create a simple web server.
// app.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
You can create a module by defining functionality in a JavaScript file, and then export the functionalities you want to expose using module.exports. For example:
// greet.js
const greet = (name) => {
console.log(`Hello, ${name}!`);
}
module.exports = greet;
Use the require function to import a module in another file. When you require a module, Node.js will look for the module in the core modules, then in the node_modules folder, and finally in the given path.
// app.js
const greet = require('./greet');
greet('World'); // Outputs: Hello, World!
When working with third-party modules, you will deal with npm and package.json. The package.json file contains metadata about your application and its dependencies. You can create a package.json file by running npm init in your project directory and following the prompts.
Here’s a brief example of a package.json file:
{
"name": "example-app",
"version": "1.0.0",
"description": "A Node.js example app",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
In this example, `express` is a dependency. You can install dependencies listed in `package.json` by running `npm install` in your project directory.
Modules are crucial in Node.js as they help in organizing code into smaller, maintainable pieces. They prevent global scope pollution and facilitate code reuse and sharing. Node.js follows the CommonJS module system and supports core, local, and third-party modules, enabling developers to build scalable and efficient applications. While this overview provides a succinct look at Node.js modules, exploring the official Node.js documentation and experimenting with creating and using various modules will deepen your understanding and proficiency in working with Node.js modules. I recommend trying to build more complex modules, utilize various core and third-party modules, and explore creating your npm packages as you become more comfortable with Node.js.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here