]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow specific imports (no-restricted-imports) |
2 | ||
3 | Imports 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 | ||
5 | Why 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 | ||
13 | This rule allows you to specify imports that you don't want to use in your application. | |
14 | ||
456be15e TL |
15 | It applies to static imports only, not dynamic ones. |
16 | ||
eb39fafa DC |
17 | ## Options |
18 | ||
19 | The syntax to specify restricted imports looks like this: | |
20 | ||
21 | ```json | |
22 | "no-restricted-imports": ["error", "import1", "import2"] | |
23 | ``` | |
24 | ||
25 | or like this: | |
26 | ||
27 | ```json | |
28 | "no-restricted-imports": ["error", { "paths": ["import1", "import2"] }] | |
29 | ``` | |
30 | ||
31 | When 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 | ||
40 | You 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 | ||
52 | or 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 | ||
66 | or 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 |
78 | or 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 | ||
92 | The custom message will be appended to the default error message. | |
eb39fafa | 93 | |
34eeec05 | 94 | To 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 | ||
104 | Examples of **incorrect** code for this rule: | |
105 | ||
106 | ```js | |
107 | /*eslint no-restricted-imports: ["error", "fs"]*/ | |
108 | ||
109 | import fs from 'fs'; | |
110 | ``` | |
111 | ||
112 | ```js | |
113 | /*eslint no-restricted-imports: ["error", "fs"]*/ | |
114 | ||
115 | export { fs } from 'fs'; | |
116 | ``` | |
117 | ||
118 | ```js | |
119 | /*eslint no-restricted-imports: ["error", "fs"]*/ | |
120 | ||
121 | export * from 'fs'; | |
122 | ``` | |
123 | ||
124 | ```js | |
125 | /*eslint no-restricted-imports: ["error", { "paths": ["cluster"] }]*/ | |
126 | ||
127 | import cluster from 'cluster'; | |
128 | ``` | |
129 | ||
130 | ```js | |
131 | /*eslint no-restricted-imports: ["error", { "patterns": ["lodash/*"] }]*/ | |
132 | ||
133 | import 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 | ||
143 | import 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 | ||
153 | import { 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 | ||
163 | import * 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 | ||
172 | import pick from 'lodash/pick'; | |
173 | ``` | |
174 | ||
eb39fafa DC |
175 | Examples of **correct** code for this rule: |
176 | ||
177 | ```js | |
178 | /*eslint no-restricted-imports: ["error", "fs"]*/ | |
179 | ||
180 | import crypto from 'crypto'; | |
181 | export { foo } from "bar"; | |
182 | ``` | |
183 | ||
184 | ```js | |
185 | /*eslint no-restricted-imports: ["error", { "paths": ["fs"], "patterns": ["eslint/*"] }]*/ | |
186 | ||
187 | import crypto from 'crypto'; | |
188 | import eslint from 'eslint'; | |
189 | export * from "path"; | |
190 | ``` | |
191 | ||
192 | ```js | |
193 | /*eslint no-restricted-imports: ["error", { paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]*/ | |
194 | ||
195 | import 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 | ||
205 | import { 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 | ||
214 | import lodash from 'lodash'; | |
215 | ``` | |
216 | ||
eb39fafa DC |
217 | ## When Not To Use It |
218 | ||
219 | Don'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. |