Skip to content
← Back to rules

node/no-exports-assign Style

🛠️ An auto-fix is available for this rule.

作用

不允许对 exports 赋值。

为什么这不好?

直接使用 exports = {} 可能会导致混淆和潜在的错误, 因为它会重新赋值 exports 对象,这可能会破坏模块导出。 更可预测且更清晰的做法是直接使用 module.exports ,或者与 exports 结合使用。

此规则旨在禁止 exports = {},但允许 module.exports = exports = {},以避免与 n/exports-style 规则的 allowBatchAssign 选项冲突。

示例

以下是此规则的错误代码示例:

js
exports = {};

以下是此规则的正确代码示例:

js
module.exports.foo = 1;
exports.bar = 2;
module.exports = {};

// 如果与 `module.exports =` 一起使用,则允许 `exports = {}`
module.exports = exports = {};
exports = module.exports = {};

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["node"],
  "rules": {
    "node/no-exports-assign": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["node"],
  rules: {
    "node/no-exports-assign": "error",
  },
});
bash
oxlint --deny node/no-exports-assign --node-plugin

版本

此规则于 v0.9.3 中添加。

参考资料