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:

  1. When the exports variable is assigned an object different from the final module.exports, all the properties of the assigned object are lost and not exported.
  2. When only module.exports is replaced, all properties assigned to the exports variable 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 alarm

Compliant 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.

Was this documentation helpful?