Why can't I output exports = {aRom 1} in node.js?

exports and module.exports in node are both empty objects. The output and import methods are as follows:

1:
// a.js
module.exports = {a:1}
// b.js
import a from "a.js"  require("a.js")
//  {a:1}

2:
// a.js
exports.a = 1
// 1 

// 
exports = {a: 1}
//  {}

exports and module.exports both point to an empty object, and exports = {a: 1} is also assigned to this empty object, but why is the output still an empty object? Ask the boss for advice. no, no, no.

Jul.21,2021

I feel that the title of the subject is not very good, because the title alone looks confused.

the confusion of the landlord is divided into several parts. The first point of
is that the reference to the module is actually the export part of the referenced module, that is, module.exports . For example, "mode 1" mentioned in your problem description. The second point of
is that exports is a shortcut to module.exports in the file-level scope of the module. So in the module, the effects of module.exports.f = () = > {} and exports.f = () = > {} are actually the same. For example, the first half of "Mode 2" mentioned in the description of the subject question.
then based on the above two points, it is mentioned in the question about the subject that if you assign a value object directly to exports , it can be understood as:


let module = {

exports:{}

}
function loadModule (module,exports) {

//TODO 
exports = { c: 345 }//exportsmodule.exports

}
loadModule (module,module.exports)

Menu