no-restricted-modules

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

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

为什么要限制模块?

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

规则详情

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

选项

lv76LUpwk80ZWNWSi3TH8kEHeeDyPfU3+L0flMfqlsbrsXCDuR5bnA7/2jvCsIJSak9407NpuTHMC15BB5hVjuZrdGDZ/WQAxHTLoFuCf8I=

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

+QThlUqRe5u8yCDMNq6Mu7PWmpZQHe8aGB+NGBO3b8rBWVZuT3PX4VIN/+cOV+wO8U1inZcU1fv5gG6nffPibNvhHLQxWhorNSriyIU5qknw9QM2CiDtIVw/4sSE0h01lkvmqvQvkN0U9HBlej3kD/wPHKY6rWtYyJTZIqWUS54=

"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"]
}]

PhpLhZx9SSLAv2GkrlI2HWEhCGhMOChrJcPhivZl/hVzeSnLd8ukBz6Fddu7hdn9GfrrGZi1yuc0s+su75f5Cd7zZ2jSNwvqMZ1vQ/Frnhs=

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

6o8DjqQ6tGHaq/pPjySZtA==

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

8qfY36ZFg9s9Ur1zRi0UlK/DeE+Sb98lqvcdesHDq6R4lSq1HukhzylF4JZV+ImToRh8zd0x2DguRetGfSMvC3NRvO7E6hxpzSek212UL5eE63v+Y0ekO3/pnjgFrqfo7Fywt68E9mAH+kvvl2vKAEZHZFjbYPeYQPbfiCJ8DIjZ6IWNF4jZ500yGA1zOeMvnL3eLOkMG+a36j/70/xfkdIb9RNBZdG8ER+sNwqgWpU=

OU8brr1GL3Xrw8uUCEPiWgkMis0QGFeviBV5mES0ALLCIT2TX1jxwcuolfAbi7bMdblmjFxAbPeM348FBX/+oZtxyTydBvherQUs29RLt7V8wHAW/SSJSHRZxk606OuRKOMWn3IN1YGXio9ekNwOBiW8sJVGhpARg++5cbvn/gkooHYM6wR7avw0S39xyF99AjMZidgOGpcHliXzuN6+YBq4IzNvFPfnHTbKXY256ig=

{
    "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"
    ]
}

示例

/mlh2HHgoNdc+E9gg/5vEO0dhGshB4hDqDONFnS/IksQxVl+JTCUZhizGeTstxop+xbkpSVN+shXPAmx+UC4iKvuT42S7x4M2s8VQV0xsnXggnoGEN3ZjqYjVMz9OqLPtCglT8mEviDzlCR/U8e4gA==

/*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');

/mlh2HHgoNdc+E9gg/5vEO0dhGshB4hDqDONFnS/IksQxVl+JTCUZhizGeTstxop+xbkpSVN+shXPAmx+UC4iNd4qC6OeoDxDfWUQqZexdcFqTmuBPBST0Ikc0SF7vbDNeAgfefW7MlBz3yGOM169w==

/*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');