]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-restricted-imports.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-restricted-imports.md
CommitLineData
eb39fafa
DC
1# Disallow specific imports (no-restricted-imports)
2
3Imports are an ES6/ES2015 standard for making the functionality of other modules available in your current module. In CommonJS this is implemented through the `require()` call which makes this ESLint rule roughly equivalent to its CommonJS counterpart `no-restricted-modules`.
4
5Why would you want to restrict imports?
6
7* Some imports might not make sense in a particular environment. For example, Node.js' `fs` module would not make sense in an environment that didn't have a file system.
8
9* Some modules provide similar or identical functionality, think `lodash` and `underscore`. Your project may have standardized on a module. You want to make sure that the other alternatives are not being used as this would unnecessarily bloat the project and provide a higher maintenance cost of two dependencies when one would suffice.
10
11## Rule Details
12
13This rule allows you to specify imports that you don't want to use in your application.
14
456be15e
TL
15It applies to static imports only, not dynamic ones.
16
eb39fafa
DC
17## Options
18
19The syntax to specify restricted imports looks like this:
20
21```json
22"no-restricted-imports": ["error", "import1", "import2"]
23```
24
25or like this:
26
27```json
28"no-restricted-imports": ["error", { "paths": ["import1", "import2"] }]
29```
30
31When using the object form, you can also specify an array of gitignore-style patterns:
32
33```json
34"no-restricted-imports": ["error", {
35 "paths": ["import1", "import2"],
36 "patterns": ["import1/private/*", "import2/*", "!import2/good"]
37}]
38```
39
40You may also specify a custom message for any paths you want to restrict as follows:
41
42```json
43"no-restricted-imports": ["error", {
44 "name": "import-foo",
45 "message": "Please use import-bar instead."
46}, {
47 "name": "import-baz",
48 "message": "Please use import-quux instead."
49}]
50```
51
52or like this:
53
54```json
55"no-restricted-imports": ["error", {
56 "paths": [{
57 "name": "import-foo",
58 "message": "Please use import-bar instead."
59 }, {
60 "name": "import-baz",
61 "message": "Please use import-quux instead."
62 }]
63}]
64```
65
66or like this if you need to restrict only certain imports from a module:
67
68```json
69"no-restricted-imports": ["error", {
70 "paths": [{
71 "name": "import-foo",
72 "importNames": ["Bar"],
73 "message": "Please use Bar from /import-bar/baz/ instead."
74 }]
75}]
76```
77
5422a9cc
TL
78or like this if you want to apply a custom message to pattern matches:
79
80```json
81"no-restricted-imports": ["error", {
82 "patterns": [{
83 "group": ["import1/private/*"],
84 "message": "usage of import1 private modules not allowed."
85 }, {
86 "group": ["import2/*", "!import2/good"],
87 "message": "import2 is deprecated, except the modules in import2/good."
88 }]
89}]
90```
91
92The custom message will be appended to the default error message.
eb39fafa 93
34eeec05 94To restrict the use of all Node.js core imports (via <https://github.com/nodejs/node/tree/master/lib>):
eb39fafa
DC
95
96```json
97 "no-restricted-imports": ["error",
98 "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"
99 ],
100```
101
102## Examples
103
104Examples of **incorrect** code for this rule:
105
106```js
107/*eslint no-restricted-imports: ["error", "fs"]*/
108
109import fs from 'fs';
110```
111
112```js
113/*eslint no-restricted-imports: ["error", "fs"]*/
114
115export { fs } from 'fs';
116```
117
118```js
119/*eslint no-restricted-imports: ["error", "fs"]*/
120
121export * from 'fs';
122```
123
124```js
125/*eslint no-restricted-imports: ["error", { "paths": ["cluster"] }]*/
126
127import cluster from 'cluster';
128```
129
130```js
131/*eslint no-restricted-imports: ["error", { "patterns": ["lodash/*"] }]*/
132
133import pick from 'lodash/pick';
134```
135
136```js
137/*eslint no-restricted-imports: ["error", { paths: [{
138 name: "foo",
139 importNames: ["default"],
140 message: "Please use the default import from '/bar/baz/' instead."
141}]}]*/
142
143import DisallowedObject from "foo";
144```
145
146```js
147/*eslint no-restricted-imports: ["error", { paths: [{
148 name: "foo",
149 importNames: ["DisallowedObject"],
150 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
151}]}]*/
152
153import { DisallowedObject as AllowedObject } from "foo";
154```
155
156```js
157/*eslint no-restricted-imports: ["error", { paths: [{
158 name: "foo",
159 importNames: ["DisallowedObject"],
160 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
161}]}]*/
162
163import * as Foo from "foo";
164```
165
5422a9cc
TL
166```js
167/*eslint no-restricted-imports: ["error", { patterns: [{
168 group: ["lodash/*"],
169 message: "Please use the default import from 'lodash' instead."
170}]}]*/
171
172import pick from 'lodash/pick';
173```
174
eb39fafa
DC
175Examples of **correct** code for this rule:
176
177```js
178/*eslint no-restricted-imports: ["error", "fs"]*/
179
180import crypto from 'crypto';
181export { foo } from "bar";
182```
183
184```js
185/*eslint no-restricted-imports: ["error", { "paths": ["fs"], "patterns": ["eslint/*"] }]*/
186
187import crypto from 'crypto';
188import eslint from 'eslint';
189export * from "path";
190```
191
192```js
193/*eslint no-restricted-imports: ["error", { paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]*/
194
195import DisallowedObject from "foo"
196```
197
198```js
199/*eslint no-restricted-imports: ["error", { paths: [{
200 name: "foo",
201 importNames: ["DisallowedObject"],
202 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
203}]}]*/
204
205import { AllowedObject as DisallowedObject } from "foo";
206```
207
5422a9cc
TL
208```js
209/*eslint no-restricted-imports: ["error", { patterns: [{
210 group: ["lodash/*"],
211 message: "Please use the default import from 'lodash' instead."
212}]}]*/
213
214import lodash from 'lodash';
215```
216
eb39fafa
DC
217## When Not To Use It
218
219Don't use this rule or don't include a module in the list for this rule if you want to be able to import a module in your project without an ESLint error or warning.