In Node.js, a module is a self-contained piece of code that performs a specific task. It can be a function, an object, or a piece of functionality that can be used in other parts of your application.
There are three types of modules in Node.js: internal, user-created, and third-party modules from npm.
Internal modules are built into Node.js and can be accessed using the require function without any additional installation. For example, the built-in http
module is used to create a web server:
const http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
In this example, we use the require()
function to load the http
module and use it to create a web server that listens on port 8080.
User-created modules are modules that you create yourself and can be included in your application using the require function. For example, you can create a module that exports a function that adds two numbers:
// math.js
function add(a, b) {
return a + b;
}
module.exports = add;
And then use it in another file:
// index.js
const add = require('./math');
console.log(add(2, 3)); // output: 5
In this example, we create a module called math.js
that exports a function called add
. We then use the require()
function to load the add
function from the math.js
module and use it to add two numbers.
Third-party modules from npm are modules that are created by other developers and can be downloaded from the npm registry using the npm package manager. For example, you can use the axios
module to make HTTP requests:
const axios = require('axios');
axios.get('<https://jsonplaceholder.typicode.com/users>')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
In this example, we use the require()
function to load the axios
module and use it to make an HTTP GET request to the jsonplaceholder
API.
To export a module from a file, you need to use the module.exports
or export
keyword. This allows you to make the module available to other parts of your application. For example, you can create a module that exports an object:
// logger.js
const logger = {
log: function(message) {
console.log(message);
}
};
module.exports = logger;
And then use it in another file: