]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-restricted-modules.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-restricted-modules.md
1 ---
2 title: no-restricted-modules
3 rule_type: suggestion
4 ---
5
6
7 This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-n`](https://github.com/eslint-community/eslint-plugin-n).
8
9 A module in Node.js is a simple or complex functionality organized in a JavaScript file which can be reused throughout the Node.js
10 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".
11
12 Why would you want to restrict a module?
13
14 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.
15
16 ## Rule Details
17
18 This rule allows you to specify modules that you don’t want to use in your application.
19
20 ## Options
21
22 The rule takes one or more strings as options: the names of restricted modules.
23
24 ```json
25 "no-restricted-modules": ["error", "foo-module", "bar-module"]
26 ```
27
28 It can also take an object with lists of `paths` and gitignore-style `patterns` strings.
29
30 ```json
31 "no-restricted-modules": ["error", { "paths": ["foo-module", "bar-module"] }]
32 ```
33
34 ```json
35 "no-restricted-modules": ["error", {
36 "paths": ["foo-module", "bar-module"],
37 "patterns": ["foo-module/private/*", "bar-module/*","!baz-module/good"]
38 }]
39 ```
40
41 You may also specify a custom message for any paths you want to restrict as follows:
42
43 ```json
44 "no-restricted-modules": ["error", {
45 "name": "foo-module",
46 "message": "Please use bar-module instead."
47 }
48 ]
49 ```
50
51 or like this:
52
53 ```json
54 "no-restricted-modules": ["error",{
55 "paths":[{
56 "name": "foo-module",
57 "message": "Please use bar-module instead."
58 }]
59 }]
60 ```
61
62 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.
63
64 To restrict the use of all Node.js core modules (via <https://github.com/nodejs/node/tree/master/lib>):
65
66 ```json
67 {
68 "no-restricted-modules": ["error",
69 "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"
70 ]
71 }
72 ```
73
74 ## Examples
75
76 Examples of **incorrect** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules:
77
78 ::: incorrect
79
80 ```js
81 /*eslint no-restricted-modules: ["error", "fs", "cluster"]*/
82
83 var fs = require('fs');
84 var cluster = require('cluster');
85 ```
86
87 :::
88
89 ::: incorrect
90
91 ```js
92 /*eslint no-restricted-modules: ["error", {"paths": ["cluster"] }]*/
93
94 var cluster = require('cluster');
95 ```
96
97 :::
98
99 ::: incorrect
100
101 ```js
102 /*eslint no-restricted-modules: ["error", { "patterns": ["lodash/*"] }]*/
103
104 var pick = require('lodash/pick');
105 ```
106
107 :::
108
109 Examples of **correct** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules:
110
111 ::: correct
112
113 ```js
114 /*eslint no-restricted-modules: ["error", "fs", "cluster"]*/
115
116 var crypto = require('crypto');
117 ```
118
119 :::
120
121 ::: correct
122
123 ```js
124 /*eslint no-restricted-modules: ["error", {
125 "paths": ["fs", "cluster"],
126 "patterns": ["lodash/*", "!lodash/pick"]
127 }]*/
128
129 var crypto = require('crypto');
130 var pick = require('lodash/pick');
131 ```
132
133 :::