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