CodePark/ questions / Modules (CommonJS & ESM)
START PRACTICINGPRACTICE
Backend question · Node.js › Modules (CommonJS & ESM)

What does main.js log?

// greet.js
exports = { hello: () => "hi" };

// main.js
const m = require("./greet");
console.log(typeof m.hello);
undefined

exports is just a local variable. At the start it points to module.exports. Reassigning exports breaks that link. require always returns module.exports, which is still the empty object. Use module.exports = { ... } instead, or add properties with exports.hello = ....