Skip to content
← Back to rules

unicorn/prefer-module Restriction

🚧 An auto-fix is planned for this rule, but not implemented at this time.

它的作用

优先使用 JavaScript 模块(ESM)而不是 CommonJS。

为什么这不好?

CommonJS 全局变量和模式(requiremoduleexports__filename__dirname) 会让代码更难迁移,并且可能阻止仅 ESM 功能。

示例

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

js
"use strict";
const foo = require("foo");
module.exports = foo;

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

js
import foo from "foo";
export default foo;

如何使用

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

json
{
  "rules": {
    "unicorn/prefer-module": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-module": "error",
  },
});
bash
oxlint --deny unicorn/prefer-module

版本

此规则于 v1.50.0 中新增。

参考资料