module.exports in Node JS
In Node js, module.export
modules are exposed on the server side and provided in CommonJS format.
Node js modules can be called as pre-built code packages that contain javascript objects and functions and can be used by external applications.
Using modules in Node JS
Modules are an essential part of writing programs because they make our lives as developers easy by allowing us to 重用代码
,,, and .调试
易于理解
管理复杂的应用程序
There are three types of modules in Node js; those modules which essentially come with the installation of Node js are called Core
modules. These modules are easy to use in our programs as we only need to access their functionality.
A typical example is HTTP
the module.
const http = require('http');
Using third-party modules in Node JS
We also have 第三方
modules that we need to install before using them in our program. These modules are usually installed using npm packages.
A typical example of such modules is the express package. Once installed, these packages are added as dependencies package.json
under the .
We can then require 关键字
import them into our program using .
npm install express
const express = require('express');
Using local modules in Node JS
Finally, we have local
modules. As the name suggests, these are modules that we create ourselves to perform various operations.
Unlike the previous two types of modules, these modules require us to first export them to make them available, and then import them into the program where we intend to implement them.
There are two widely used export and import modules on the server side, especially in Node js. These are CommonJS and ESM
.
The CommonJS format allows us to create modules in unique contexts in different Node js files.
Modules that conform to this format can be called CommonJs
modules. In this format, we use module.exports
objects to expose a module and use the default require
keyword to import them into the program.
On the other hand, ESM
the __register__ format uses export
the __name__ keyword to identify export
modules and imports them import
according to the __register__ ES6
syntax using the __register__ keyword.
Use in Node JSmodule.experts
Using module.exports
, we can export literals
, , functions
, classes
and objects
. keyword
module stands for current
module, while the keyword export exposes any object as a module.
module.exports
We can export them as a module by simply assigning them to . This is because an export is itself an object, so any literal assigned to it will be exported as a module.
In the following example, we create a Node js application, our main file is index.js
and description.js
contains the text we want to export as a module.
Use the one that contains text description.js
.
let tesla = "Largest electric car manufacturer";
module.exports = tesla;
Use index.js
as our main file.
var desc_tesla = require('./description.js');
console.log(desc_tesla);
Output:
Largest electric car manufacturer
In the Node application above, we exported a String literal
and imported it by specifying description.js
the path to the root directory where index.js
.
We can also export functions as modules, just create a function as we normally do in javascript and assign the name of the function to module.exports
.
Now that the module system knows that we want to expose the function as a module, we can index.js
access it by simply requiring it in our <module> file as shown below.
Use the one that contains text description.js
.
function factorial(n) {
if ((n == 0) || (n === 1)) {
return 1;
} else {
return (n * factorial(n-1));
}
}
module.exports = factorial;
Use index.js
as our main file.
var factorial = require('./description.js');
console.log(factorial(7));
Output:
5040
Similarly, we can export objects as modules. Objects are independent entities with properties and types.
We can assign the object to module.exports
or assign the object name to module.exports
. Once we have exposed the object, we can use the following require()
method to access it at our desired location.
Use the one that contains text description.js
.
const car = {
make: 'Tesla',
model: 'S plaid',
top_speed: '200mph',
year: 2019
}
module.exports = car;
Use index.js
as our main file.
var car = require('./description.js');
console.log(car.make);
console.log(car.model);
console.log(car.top_speed);
Output:
Tesla
S plaid
200mph
Once we have imported the object index.js
, we can access the object's properties using dot notation.
In the examples above, we have been using ./description.js
import-module where ./
specifying description.js
is located in the root folder.
However, this may not always be the case; in some cases, the modules may be located in different folders. We need to specify the absolute location of importing such modules, which ./
begin with , pointing to the root folder.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Throwing Errors in Node.js
Publish Date:2025/04/17 Views:164 Category:Node.js
-
This article will explain how to throw errors in Node.js. Throwing Errors in Node.js Errors are statements that do not allow the system to function properly. Errors in Node.Js are handled through exceptions, which are created with the help
Solve the Cannot Find Module error in Node.js
Publish Date:2025/04/17 Views:70 Category:Node.js
-
In this article, we will learn how to fix the Cannot find module error in Node.js. package.json File Before diving into the solution, we will first try to understand the package.json file and why we need it. The package.json file is the roo
Multithreading in Node.js
Publish Date:2025/04/17 Views:112 Category:Node.js
-
In Node.js, the term multithreading does not apply because Node.js is designed to run in a single-threaded event loop. However, Node.js is built on top of the JavaScript language, which is single-threaded by default. However, Node.js provid
Using jQuery in Node.js
Publish Date:2025/04/17 Views:51 Category:Node.js
-
jQuery is a popular JavaScript library that is widely used to build web applications. It provides a rich set of APIs for interacting with the DOM, making HTTP requests, handling events, etc. Node.js is a JavaScript runtime that allows devel
Node.js sends files to the client
Publish Date:2025/04/17 Views:70 Category:Node.js
-
In this article, we will learn how to send files to the client in Node.js using Express. Sending files using Express in Node.js Express.js or Express is a backend web utility framework for Node.js. Express is a Node.js web application frame
HTTP POST request in Node.js
Publish Date:2025/04/17 Views:131 Category:Node.js
-
In this article, we will learn how to use Node.js to make a post request using a third-party package. HTTP Post Request in Node.js The HTTP POST method creates or adds resources on the server. The key difference between POST and PUT request
Reading Files in Node.js
Publish Date:2025/04/17 Views:138 Category:Node.js
-
In this short article, we will learn how to read files in Node.js. Reading Files in Node.js fs The module provides many useful functions to access and interact with the file system. fs One special feature of the module is that all methods a
AJAX calls in Node.js
Publish Date:2025/04/17 Views:102 Category:Node.js
-
Representational State Transfer is abbreviated as REST . An API or Web API (Application Programming Interface) that complies with the restrictions and limitations of the REST architectural style and allows interaction with RESTful web servi
Executing Shell Scripts in Node.js
Publish Date:2025/04/17 Views:139 Category:Node.js
-
In this short article, we will learn how to execute shell scripts using Node.js. Execute Shell Scripts in Node.js using the shelljs module ShellJS is a portable implementation of Unix shell commands on top of the Node.js API. We can use it