no-restricted-modules

require 加载时禁止指定模块

该规则在 ESLint v7.0.0 中已弃用。请使用 eslint-plugin-n 中的相应规则。

¥This rule was deprecated in ESLint v7.0.0. Please use the corresponding rule in eslint-plugin-n.

Node.js 中的模块是组织在 JavaScript 文件中的简单或复杂功能,可以在整个 Node.js 应用中重用。在 Node.js/CommonJS 中使用关键字 require 将模块导入应用。这样,你可以在加载的模块名称未预定义/静态的情况下进行动态加载,或者仅在模块为 “确实需要” 时有条件地加载模块。

¥A module in Node.js is a simple or complex functionality organized in a JavaScript file which can be reused throughout the Node.js application. The keyword require is used in Node.js/CommonJS to import modules into an application. This way you can have dynamic loading where the loaded module name isn’t predefined /static, or where you conditionally load a module only if it’s “truly required”.

为什么要限制模块?

¥Why would you want to restrict a module?

如果你想限制开发者可以使用的可用方法,那么禁止使用特定的 Node.js 模块可能会很有用。例如,如果你想禁止文件系统访问,你可以阻止使用 fs 模块。

¥Disallowing usage of specific Node.js modules can be useful if you want to limit the available methods a developer can use. For example, you can block usage of the fs module if you want to disallow file system access.

规则详情

¥Rule Details

此规则允许你指定不想在应用中使用的模块。

¥This rule allows you to specify modules that you don’t want to use in your application.

选项

¥Options

该规则将一个或多个字符串作为选项:受限模块的名称。

¥The rule takes one or more strings as options: the names of restricted modules.

"no-restricted-modules": ["error", "foo-module", "bar-module"]

它也可以接受一个带有 paths 和 gitignore 风格的 patterns 字符串列表的对象。

¥It can also take an object with lists of paths and gitignore-style patterns strings.

"no-restricted-modules": ["error", { "paths": ["foo-module", "bar-module"] }]
"no-restricted-modules": ["error", {
    "paths": ["foo-module", "bar-module"],
    "patterns": ["foo-module/private/*", "bar-module/*","!baz-module/good"]
}]

你还可以为要限制的任何路径指定自定义消息,如下所示:

¥You may also specify a custom message for any paths you want to restrict as follows:

"no-restricted-modules": ["error", {
  "name": "foo-module",
  "message": "Please use bar-module instead."
  }
]

或像这样:

¥or like this:

"no-restricted-modules": ["error",{
"paths":[{
  "name": "foo-module",
  "message": "Please use bar-module instead."
  }]
}]

自定义消息将附加到默认错误消息中。请注意,你不能为受限模式指定自定义错误消息,因为特定模块可能匹配多个模式。

¥The custom message will be appended to the default error message. Please note that you may not specify custom error messages for restricted patterns as a particular module may match more than one pattern.

限制使用所有 Node.js 核心模块(通过 https://github.com/nodejs/node/tree/master/lib):

¥To restrict the use of all Node.js core modules (via https://github.com/nodejs/node/tree/master/lib):

{
    "no-restricted-modules": ["error",
        "assert","buffer","child_process","cluster","crypto","dgram","dns","domain","events","freelist","fs","http","https","module","net","os","path","punycode","querystring","readline","repl","smalloc","stream","string_decoder","sys","timers","tls","tracing","tty","url","util","vm","zlib"
    ]
}

示例

¥Examples

使用示例 "fs", "cluster", "lodash" 受限模块的此规则的错误代码示例:

¥Examples of incorrect code for this rule with sample "fs", "cluster", "lodash" restricted modules:

在线运行
/*eslint no-restricted-modules: ["error", "fs", "cluster"]*/

var fs = require('fs');
var cluster = require('cluster');
在线运行
/*eslint no-restricted-modules: ["error", {"paths": ["cluster"] }]*/

var cluster = require('cluster');
在线运行
/*eslint no-restricted-modules: ["error", { "patterns": ["lodash/*"] }]*/

var pick = require('lodash/pick');

此规则的正确代码示例以及示例 "fs", "cluster", "lodash" 受限模块:

¥Examples of correct code for this rule with sample "fs", "cluster", "lodash" restricted modules:

在线运行
/*eslint no-restricted-modules: ["error", "fs", "cluster"]*/

var crypto = require('crypto');
在线运行
/*eslint no-restricted-modules: ["error", {
    "paths": ["fs", "cluster"],
    "patterns": ["lodash/*", "!lodash/pick"]
}]*/

var crypto = require('crypto');
var pick = require('lodash/pick');

版本

此规则是在 ESLint v0.6.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号