← Back to Node.js, page 2

nodejs-0022 · Easy fresher

What is the difference between module.exports and exports in CommonJS?

exports is a variable that starts as a reference to module.exports. Adding properties to either works. Assigning a new value to exports only changes the local variable, and require returns module.exports, so the assignment is lost. To export a function or a class, assign it to module.exports.

Full answer

Inside every CommonJS file, Node wraps your code in a function and gives it exports, require, module, __filename and __dirname. At the start, exports and module.exports point to the same empty object.

require() always returns module.exports, never the exports variable.

That is why these two forms behave in different ways:

  • exports.add = ... and module.exports.add = ...: both add a property to the shared object. Both work.
  • module.exports = function () {}: replaces the exported object. This works.
  • exports = function () {}: only repoints the local variable. module.exports is still the old empty object. The importer gets {}.

Use exports.name = value for several named exports. Use module.exports = value when the module has a single main export, such as a class or a function. Do not mix both styles in one file, because assigning module.exports later throws away the properties you added to exports.

ES modules do not have this issue because export statements are syntax, not objects.

javascript
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'exports-'));
const write = (name, text) => fs.writeFileSync(path.join(dir, name), text);
const load = (name) => require(path.join(dir, name));

write('ok-property.js', "exports.hello = () => 'hello';");
write('ok-replace.js', "module.exports = () => 'hello';");
write('lost.js', "exports = () => 'hello'; // only changes the local variable");
write('mixed.js', "exports.a = 1; module.exports = { b: 2 };");

console.log('exports.hello:        ', typeof load('ok-property.js').hello);
console.log('module.exports = fn:  ', typeof load('ok-replace.js'));
console.log('exports = fn (lost):  ', JSON.stringify(load('lost.js')));
console.log('mixed styles:         ', JSON.stringify(load('mixed.js')));

fs.rmSync(dir, { recursive: true });

The 'lost' module exports an empty object because the assignment never reaches module.exports. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): exports.hello: function module.exports = fn: function exports = fn (lost): {} mixed styles: {"b":2}

Key points

  • require returns module.exports.
  • exports and module.exports start as the same object.
  • Assigning to exports breaks the link.
  • Use module.exports for a single export, exports.x for many.

Common mistakes

  • Writing exports = something and wondering why require returns {}.
  • Mixing exports.x and module.exports = in one file.
  • Exporting a class with exports.MyClass = and then calling require() as a function.
  • Thinking the same rules apply to ES modules.

Follow-ups

  • Why does Node wrap modules in a function?
  • How does the same example look with ES modules?
  • How do circular dependencies interact with module.exports replacement?

Tips

  • Draw two arrows from the names to one object, then cut one of them.
  • State that require returns module.exports in one short sentence.
  • Run the snippet before an interview. It takes a minute and it sticks.