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