no-require-imports
禁止调用
require()
.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/recommended"
可启用此规则。
根据你的 TSConfig 设置以及你是编写 ES 模块还是 CommonJS,TS 可能允许同时使用 import
和 require()
,即使在单个文件中也是如此。
¥Depending on your TSConfig settings and whether you're authoring ES Modules or CommonJS, TS may allow both import
and require()
to be used, even within a single file.
此规则强制使用较新的 ES 模块 import
语法而不是 CommonJS require()
。
¥This rule enforces that you use the newer ES Module import
syntax over CommonJS require()
.
- 扁平配置
- 旧版配置
export default tseslint.config({
rules: {
"@typescript-eslint/no-require-imports": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-require-imports": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
const lib1 = require('lib1');
const { lib2 } = require('lib2');
import lib3 = require('lib3');
Open in Playgroundimport * as lib1 from 'lib1';
import { lib2 } from 'lib2';
import * as lib3 from 'lib3';
Open in Playground选项
该规则接受以下选项:
type Options = [
{
/** 允许从中获取的导入路径模式。 */
allow?: string[];
/** 允许在导入声明中使用 `require` 语句。 */
allowAsImport?: boolean;
},
];
const defaultOptions: Options = [{ allow: [], allowAsImport: false }];
¥Options
allow
允许从中获取的导入路径模式。 Default: []
.
这些字符串将使用 u
标志编译为正则表达式,并用于针对导入的路径进行测试。常见用例是允许导入 package.json
。这是因为 package.json
通常位于 TS 根目录之外,因此静态导入它会导致根目录冲突,尤其是在启用 resolveJsonModule
的情况下。如果你的环境不支持 JSON 模块,你还可以使用它来允许导入任何 JSON,或者在 import
语句无法工作的其他情况下使用它。
¥These strings will be compiled into regular expressions with the u
flag and be used to test against the imported path. A common use case is to allow importing package.json
. This is because package.json
commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with resolveJsonModule
enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where import
statements cannot work.
使用 { allow: ['/package\\.json$'] }
:
¥With { allow: ['/package\\.json$'] }
:
- ❌ 错误
- ✅ 正确
console.log(require('../data.json').version);
Open in Playgroundconsole.log(require('../package.json').version);
Open in PlaygroundallowAsImport
允许在导入声明中使用 require
语句。 Default: false
.
当设置为 true
时,不会报告 import ... = require(...)
声明。如果你使用某些需要严格 CommonJS 互操作语义的模块选项(例如 verbatimModuleSyntax),这将非常有用。
¥When set to true
, import ... = require(...)
declarations won't be reported.
This is beneficial if you use certain module options that require strict CommonJS interop semantics, such as verbatimModuleSyntax.
使用 { allowAsImport: true }
:
¥With { allowAsImport: true }
:
- ❌ 错误
- ✅ 正确
var foo = require('foo');
const foo = require('foo');
let foo = require('foo');
Open in Playgroundimport foo = require('foo');
import foo from 'foo';
Open in Playground与 CommonJS 一起使用
¥Usage with CommonJS
虽然此规则主要是为了促进 ES 模块语法,但在编写 CommonJS 模块时启用此规则仍然有意义。
¥While this rule is primarily intended to promote ES Module syntax, it still makes sense to enable this rule when authoring CommonJS modules.
如果你更喜欢使用 TypeScript 的内置 import ... from ...
ES 模块语法,该语法在输出 CommonJS 时在转译过程中转换为 require()
调用,你可以使用规则的默认行为。
¥If you prefer to use TypeScript's built-in import ... from ...
ES Module syntax, which is transformed to require()
calls during transpilation when outputting CommonJS, you can use the rule's default behavior.
如果你更喜欢使用 require()
语法,我们建议你在启用 allowAsImport
的情况下使用此规则。这样,你仍然可以强制使用 import ... = require(...)
,而不是裸 require()
调用,因为 TypeScript 不会对其进行静态分析。我们没有直接禁止使用 ES 模块语法的方法;如果你发现自己处于需要这种情况的情况,请考虑使用 TypeScript 的 verbatimModuleSyntax
选项。
¥If, instead, you prefer to use require()
syntax, we recommend you use this rule with allowAsImport
enabled.
That way, you still enforce usage of import ... = require(...)
rather than bare require()
calls, which are not statically analyzed by TypeScript.
We don't directly a way to prohibit ES Module syntax from being used; consider instead using TypeScript's verbatimModuleSyntax
option if you find yourself in a situation where you would want this.
何时不使用它
¥When Not To Use It
如果你正在编写 CommonJS 模块,并且你的项目经常使用动态 require
,那么此规则可能不适用于你。否则 allowAsImport
选项可能适合你的需求。
¥If you are authoring CommonJS modules and your project frequently uses dynamic require
s, then this rule might not be applicable to you.
Otherwise the allowAsImport
option probably suits your needs.
如果只有项目的子集使用动态 require
,那么你可能考虑在特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
¥If only a subset of your project uses dynamic require
s then you might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
相关
¥Related To