Assignments to exports variable and its properties should be properly exported
- MISUSED_EXPORTS_VAR
- Error
- Medium
- nodejs
This rule applies when an assignment to exports variable or its property is not properly exported.
A CommonJS module exports the property value of module.exports, not the value of exports variable.
Although module.exports and exports initially have the same object, programmers may assign a new object to module.exports or exports. Caution is needed to avoid the following cases:
- When the
exportsvariable is assigned an object different from the finalmodule.exports, all the properties of the assigned object are lost and not exported. - When only
module.exportsis replaced, all properties assigned to theexportsvariable are lost and not exported.
It is recommended to use module.exports only or make module.exports and exports always have the same value.
Noncompliant Code Example
View with compliant examples side by side// Example 1
exports = { // MISUSED_EXPORTS_VAR alarm
add: function (a, b) { return a + b; }
};
// Example 2
module.exports = {
add: function (a, b) { return a + b; }
};
exports.mult = function (a, b) { return a * b; }; // MISUSED_EXPORTS_VAR alarmCompliant Code Example
View with noncompliant examples side by side// Example 1
module.exports = {
add: function (a, b) { return a + b; }
};
// Example 2
module.exports = {
add: function (a, b) { return a + b; }
};
module.exports.mult = function (a, b) { return a * b; };Version
This rule was introduced in DeepScan 1.31.0.