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
exportsUnexpected access to 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 }]| 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":
example.jswith
"exports"2 problemsmodule.exportsUnexpected access to module.exports. = { parse };
exportsUnexpected assignment to exports. = { parse };example.jswith
"exports"No problemsexports.parse = parse;With allowBatchAssign, you can replace the exported object and keep exports pointing at it:
example.jswith
"exports", { "allowBatchAssign": true }No problemsmodule.exports = exports = function parse(text) {};
exports.version = "1.0.0";example.jswith
"exports"2 problemsmodule.exportsUnexpected access to module.exports. = exportsUnexpected assignment to exports. = function parse(text) {};
exports.version = "1.0.0";