n/exports-style

Enforce either module.exports or exports in CommonJS files

Also called node/exports-style

A CommonJS file can export through module.exports or through its shorthand exports. Both start as the same object, but once you assign a new value to module.exports, anything you add to exports is no longer exported. Sticking to one of the two everywhere avoids that.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"n/exports-style" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"n/exports-style" = [
  "error",
  "module.exports", # "module.exports" or "exports"
  {
    allowBatchAssign = false,
  },
]

Examples#

example.js1 problem
exports.parse = function parse(text) {
  return JSON.parse(text);
};
example.jsNo problems
module.exports.parse = function parse(text) {
  return JSON.parse(text);
};

Options#

bunfig.toml
[lint.rules]
"n/exports-style" = ["error", "exports", { allowBatchAssign = true }]
OptionWhat it does
"module.exports" (default)Report every use of exports.
"exports"Report every use of module.exports, and every assignment to exports itself.

The second option is an object:

OptionDefaultWhat it does
allowBatchAssignfalseAllow one statement that assigns to both, such as module.exports = exports = value.

With "exports":

example.jswith "exports"2 problems
module.exports = { parse };
exports = { parse };
example.jswith "exports"No problems
exports.parse = parse;

With allowBatchAssign, you can replace the exported object and keep exports pointing at it:

example.jswith "exports", { "allowBatchAssign": true }No problems
module.exports = exports = function parse(text) {};
exports.version = "1.0.0";
example.jswith "exports"2 problems
module.exports = exports = function parse(text) {};
exports.version = "1.0.0";