Home > Web Front-end > Front-end Q&A > What are the two ways to export modules in node?

What are the two ways to export modules in node?

WBOY
Release: 2022-04-22 14:57:29
Original
4158 people have browsed it

Two ways for node to export modules: 1. Using exports, this method can be exported by adding attributes, and multiple members can be exported; 2. Using "module.exports", this method can be exported directly through When exporting a module by assigning a value to "module.exports", only a single member can be exported.

What are the two ways to export modules in node?

The operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.

What are the two ways for node to export a module?

There are two ways to export a module.

  • exports can export multiple members

  • module.exports can only export a single member, and the later ones will overwrite the previous

##1, module.exports

We can export the module directly by assigning values ​​to module.exports

module.exports can export a single member

The functions of exports can be realized by module.exports

2. exports

The exports variable is a reference to module.exports provided by node.

exports is exported by adding attributes, and only one object can be exported.

If you directly point the exports variable to a value, it will not affect module.exports, but this is equivalent to cutting off the connection between exports and module.exports.

Examples are as follows:

// 定义方法,常量
const myPI = 3.14
const add = (a,b) => a + b;
// 导出,两种方法任意都可以
// 方法一:
exports.myPI = myPI
exports.add = add
// 方法二:
module.exports.myPI = myPI
module.exports.add = add
// 方法二(变形)
module.exports  = {
    myPI,
    add
}
Copy after login

Recommended learning: "

nodejs video tutorial"

The above is the detailed content of What are the two ways to export modules in node?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template