1 # Disallow Node.js modules (no-restricted-modules)
3 This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-node`](https://github.com/mysticatea/eslint-plugin-node).
5 A module in Node.js is a simple or complex functionality organized in a JavaScript file which can be reused throughout the Node.js
6 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".
8 Why would you want to restrict a module?
10 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.
14 This rule allows you to specify modules that you don’t want to use in your application.
18 The rule takes one or more strings as options: the names of restricted modules.
21 "no-restricted-modules": ["error", "foo-module", "bar-module"]
24 It can also take an object with lists of `paths` and gitignore-style `patterns` strings.
27 "no-restricted-modules": ["error", { "paths": ["foo-module", "bar-module"] }]
31 "no-restricted-modules": ["error", {
32 "paths": ["foo-module", "bar-module"],
33 "patterns": ["foo-module/private/*", "bar-module/*","!baz-module/good"]
37 You may also specify a custom message for any paths you want to restrict as follows:
40 "no-restricted-modules": ["error", {
42 "message": "Please use bar-module instead."
50 "no-restricted-modules": ["error",{
53 "message": "Please use bar-module instead."
58 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.
60 To restrict the use of all Node.js core modules (via <https://github.com/nodejs/node/tree/master/lib>):
64 "no-restricted-modules": ["error",
65 "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"
72 Examples of **incorrect** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules:
75 /*eslint no-restricted-modules: ["error", "fs", "cluster"]*/
77 var fs = require('fs');
78 var cluster = require('cluster');
82 /*eslint no-restricted-modules: ["error", {"paths": ["cluster"] }]*/
84 var cluster = require('cluster');
88 /*eslint no-restricted-modules: ["error", { "patterns": ["lodash/*"] }]*/
90 var pick = require('lodash/pick');
93 Examples of **correct** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules:
96 /*eslint no-restricted-modules: ["error", "fs", "cluster"]*/
98 var crypto = require('crypto');
102 /*eslint no-restricted-modules: ["error", {
103 "paths": ["fs", "cluster"],
104 "patterns": ["lodash/*", "!lodash/pick"]
107 var crypto = require('crypto');
108 var pick = require('lodash/pick');