Home > Article > Web Front-end > Let’s talk about nodejs module export method
Node.js is a very popular server-side JavaScript runtime environment, which can run JavaScript code directly on our server side. In Node.js, modules are a technology used to organize and encapsulate code.
The export method allows us to expose the parts we want from a module for use by other modules. In Node.js, export methods in modules usually have the following methods.
1. module.exports
The most commonly used export method is module.exports. By setting module.exports to a function, object, class, constant, etc., we can use it in other modules.
For example, we have a module called "sum.js" that defines a function that adds two numbers:
function add(a, b) { return a + b; } module.exports = add;
In another module, we can require Method to load and use the functions defined in this module:
const sum = require('./sum.js'); console.log(sum(1, 2)); // 输出 3
We can also export any number of functions, objects, classes, constants, etc. in a module through module.exports:
function add(a, b) { return a + b; } function subtract(a, b) { return a - b; } const PI = 3.14; module.exports = { add: add, subtract: subtract, PI: PI };
In other modules, we can load and use the content exported in the above modules in the following way:
const { add, subtract, PI } = require('./math.js'); console.log(add(1, 2)); // 输出 3 console.log(subtract(5, 3)); // 输出 2 console.log(PI); // 输出 3.14
2. Exports
Another export method is the exports object. In the Node.js module system, module.exports and exports are actually two aliases for the same object. Therefore, we can export the content in the module by modifying the exports object.
For example, we modify the "sum.js" module to export the add function through exports:
exports.add = function(a, b) { return a + b; }
In other modules, we can load and use the content in this module in the following ways:
const sum = require('./sum.js'); console.log(sum.add(1, 2)); // 输出 3
exports can also be used to export objects, for example:
exports.person = { name: '张三', age: 20 };
In other modules, we can load and use the objects exported in this module in the following way:
const person = require('./person.js'); console.log(person.name); // 输出 张三 console.log(person.age); // 输出 20
It should be noted that when we use both the exports object and module.exports in a module, the exports object will be ignored.
3. require
In addition to exporting content, we can also introduce content from other modules into the module. In Node.js, we can use the require method to introduce other modules.
For example, we have a module called "calculator.js" which depends on the "sum.js" module:
const sum = require('./sum.js'); function multiply(a, b) { let result = 0; for (let i = 0; i < b; i++) { result = sum.add(result, a); } return result; } module.exports = { multiply: multiply };
In other modules, we can load and Use the functions exported in the above module:
const calculator = require('./calculator.js'); console.log(calculator.multiply(5, 3)); // 输出 15
It should be noted that when we introduce other modules in a module, we can directly use the content exported in the imported module without using the module name. access.
Summary
The above is the method in the export module in Node.js. While the basics are covered here, the complexity of the Node.js module system goes beyond that. In practical applications, we also need to have an in-depth understanding of the Node.js module loading mechanism, optimization strategies and other details. I hope you can get inspiration from this article and have a deeper understanding and application of Node.js module export method.
The above is the detailed content of Let’s talk about nodejs module export method. For more information, please follow other related articles on the PHP Chinese website!