Skip to content
← Back to rules

node/exports-style 风格

An auto-fix is available for this rule.

功能

强制使用 module.exportsexports

为什么这是不好的?

默认情况下,module.exportsexports 是同一个实例。但如果其中一个被修改,它们就会变成不同的实例。

js
module.exports = {
  foo: 1,
};

exports.bar = 2;

在这种情况下,exports.bar 将会丢失,因为只有 module.exports 的实例会被导出。

示例

"module.exports" 选项的错误代码示例:

js
exports.foo = 1;
exports.bar = 2;

"module.exports" 选项的正确代码示例:

js
module.exports = {
  foo: 1,
  bar: 2,
};
module.exports.baz = 3;

"exports" 选项的错误代码示例:

js
module.exports = {
  foo: 1,
  bar: 2,
};
module.exports.baz = 3;

"exports" 选项的正确代码示例:

js
exports.foo = 1;
exports.bar = 2;

配置

第 1 个选项

类型:"module.exports" | "exports"

"module.exports"

要求使用 module.exports,并禁止使用 exports

"exports"

要求使用 exports,并禁止使用 module.exports

第 2 个选项

此选项是一个包含以下属性的对象:

allowBatchAssign

类型:boolean

默认值:false

如果此选项设置为 true,则允许使用 module.exports = exports = obj

如何使用

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

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

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

版本

此规则在 v1.76.0 中添加。

参考资料