Understanding of exports in the module of Node.js

  node.js, question

Js

var i = 0;
 
 function count() {
 Return plus i;
 bracket
 
 exports.count = count;

Then load the module through main.js

var counter1 = require('./util/count');
 
 console.log(counter1.count());

But i don’t understand the meaning of the last sentence of counter.js

exports.count = count;

This sentence does not mean to assign the function count to exports.count? As mentioned in “javascript Advanced Programming” earlier, writing only the name of the function without parentheses represents the function but does not execute it.

Since the export is a function, why count.count () be called through this function? Do you mean the whole js global object or just a function?

I have been wondering for a long time, thank you ~

Let me make a simple explanation, which may be easier for you to understand.

var i = 0;
 function count() {
 Return plus i;
 bracket
 exports.count = count;  //This sentence is actually saying that I have a count method in counter.js that you can use.
var counter1 = require('./util/count');  //main gets an instance called counter1, njs is introduced through require
 
 console.log(counter1.count());  //Access the count method in this instance.

Although this explanation is not very professional, it should be easy to understand and hopefully can help you.