Home > Article > Web Front-end > How to set up module in nodejs
Node.js is an event-driven asynchronous I/O framework that is rapidly evolving into a JavaScript-based server-side development tool. The core idea of Node.js is modular programming, which makes code highly reusable and modular. By using modular programming, Node.js can better split the program. Each module can be developed, tested, and maintained independently, simplifying the development process and debugging.
This article will introduce how to set up and use modules in Node.js.
1. What is a module
A module refers to a piece of reusable code, usually encapsulated into a separate file. Each module has its own interface and implementation details. Using modules can make the code structure clearer and simpler and increase code reusability.
Modular programming refers to splitting a program into small modules that depend on each other. These modules can be reused in other programs. This is the core idea of Node.js. Using modular programming can reduce program complexity and enhance program maintainability and readability.
2. How to set up modules in Node.js
In order to use modular programming in Node.js, we need to understand how to set up and use modules. The setup of the module is very simple, just follow the following steps:
(1) Create a .js file containing functions or variables
(2) Use the module.exports statement to output the code as a module
(3) Use the require statement to load the module in the file that needs to call the module
Here is the content of each step:
(1) Create a containing function Or the .js file of the variable
The module file can be any name, but we usually create the file using the module name, such as my-module.js
(2) Use the module.exports statement to output the code as Module
In order to make our module code accessible to other files, we need to use the module.exports statement to export the code as a module. Just add the module.exports = {variable name/function name} statement at the end of the code. For example:
function hello(name){
console.log("Hello," + name);
}
module.exports = hello;
(3) In the file that needs to call the module Loading modules using the require statement
In order to use the module that has been created, we need to load it into the file we need to call. Just use the require statement, for example:
const myModule = require('./my-module');
myModule('world');
In Node.js, We can provide the relative path or absolute path of the module in the require statement.
Example:
If you have two files A and B, and the function 'add' is defined in file A and is to be used by file B, the setting method is as follows:
In file A, the add function is defined:
const add = function(a, b) {
return a + b;
}
module.exports.add = add;
In file B, you need to add a require statement at the top to tell the Node.js engine that we need to reference the content in file A:
const a = require('./A');
//Call the add function in file A
console.log(a.add(2,3));
Enter node B in the command line tool, you will see the output: 5
3. Types of modules
In Node.js, there are three different types of modules to choose from.
(1) System built-in modules - This type of module is included directly in the core of Node.js and can be used by calling them directly. For example, http and fs modules.
(2) File modules - File modules refer to .js files stored on disk, they are called "file modules" in Node.js.
(3) Custom module - This is a completely customized module type that can be defined and output in a file, or customized through JavaScript in code. These custom modules can significantly improve the reusability of programs and the maintainability of writing.
4. Summary
Using modular programming can reduce the complexity of the program and enhance the maintainability and readability of the program. In Node.js, setting up modules is very simple, just follow the specifications when creating files. It should be noted that if we want to import a module, they must be in the same directory or one of the parent directories.
Finally, I wish you success in creating and using your custom module in Node.js.
The above is the detailed content of How to set up module in nodejs. For more information, please follow other related articles on the PHP Chinese website!