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
Reported:
exports.parse = function parse(text) {
return JSON.parse(text);
};
Not reported:
module.exports.parse = function parse(text) {
return JSON.parse(text);
};
Options
bunfig.toml
[lint.rules]
"n/exports-style" = ["error", "exports", { allowBatchAssign = true }]
| Option | What 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:
| Option | Default | What it does |
|---|---|---|
allowBatchAssign | false | Allow one statement that assigns to both, such as module.exports = exports = value. |
With "exports":
module.exports = { parse };
exports = { parse };
exports.parse = parse;
With allowBatchAssign, you can replace the exported object and keep exports pointing at it:
module.exports = exports = function parse(text) {};
exports.version = "1.0.0";
module.exports = exports = function parse(text) {};
exports.version = "1.0.0";