2 title: no-restricted-imports
7 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`.
9 Why would you want to restrict imports?
11 * 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.
13 * 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.
17 This rule allows you to specify imports that you don't want to use in your application.
19 It applies to static imports only, not dynamic ones.
23 The syntax to specify restricted imports looks like this:
26 "no-restricted-imports": ["error", "import1", "import2"]
32 "no-restricted-imports": ["error", { "paths": ["import1", "import2"] }]
35 When using the object form, you can also specify an array of gitignore-style patterns:
38 "no-restricted-imports": ["error", {
39 "paths": ["import1", "import2"],
40 "patterns": ["import1/private/*", "import2/*", "!import2/good"]
44 You may also specify a custom message for any paths you want to restrict as follows:
47 "no-restricted-imports": ["error", {
49 "message": "Please use import-bar instead."
52 "message": "Please use import-quux instead."
59 "no-restricted-imports": ["error", {
62 "message": "Please use import-bar instead."
65 "message": "Please use import-quux instead."
70 or like this if you need to restrict only certain imports from a module:
73 "no-restricted-imports": ["error", {
76 "importNames": ["Bar"],
77 "message": "Please use Bar from /import-bar/baz/ instead."
82 or like this if you want to apply a custom message to pattern matches:
85 "no-restricted-imports": ["error", {
87 "group": ["import1/private/*"],
88 "message": "usage of import1 private modules not allowed."
90 "group": ["import2/*", "!import2/good"],
91 "message": "import2 is deprecated, except the modules in import2/good."
96 The custom message will be appended to the default error message.
98 Pattern matches can also be configured to be case-sensitive:
101 "no-restricted-imports": ["error", {
103 "group": ["import1/private/prefix[A-Z]*"],
104 "caseSensitive": true
109 Pattern matches can restrict specific import names only, similar to the `paths` option:
112 "no-restricted-imports": ["error", {
114 "group": ["utils/*"],
115 "importNames": ["isEmpty"],
116 "message": "Use 'isEmpty' from lodash instead."
121 To restrict the use of all Node.js core imports (via <https://github.com/nodejs/node/tree/master/lib>):
124 "no-restricted-imports": ["error",
125 "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"
131 Examples of **incorrect** code for this rule:
136 /*eslint no-restricted-imports: ["error", "fs"]*/
146 /*eslint no-restricted-imports: ["error", "fs"]*/
148 export { fs } from 'fs';
156 /*eslint no-restricted-imports: ["error", "fs"]*/
166 /*eslint no-restricted-imports: ["error", { "paths": ["cluster"] }]*/
168 import cluster from 'cluster';
176 /*eslint no-restricted-imports: ["error", { "patterns": ["lodash/*"] }]*/
178 import pick from 'lodash/pick';
186 /*eslint no-restricted-imports: ["error", { paths: [{
188 importNames: ["default"],
189 message: "Please use the default import from '/bar/baz/' instead."
192 import DisallowedObject from "foo";
200 /*eslint no-restricted-imports: ["error", { paths: [{
202 importNames: ["DisallowedObject"],
203 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
206 import { DisallowedObject } from "foo";
208 import { DisallowedObject as AllowedObject } from "foo";
210 import { "DisallowedObject" as AllowedObject } from "foo";
218 /*eslint no-restricted-imports: ["error", { paths: [{
220 importNames: ["DisallowedObject"],
221 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
224 import * as Foo from "foo";
232 /*eslint no-restricted-imports: ["error", { patterns: [{
234 message: "Please use the default import from 'lodash' instead."
237 import pick from 'lodash/pick';
245 /*eslint no-restricted-imports: ["error", { patterns: [{
246 group: ["foo[A-Z]*"],
250 import pick from 'fooBar';
258 /*eslint no-restricted-imports: ["error", { patterns: [{
260 importNames: ['isEmpty'],
261 message: "Use 'isEmpty' from lodash instead."
264 import { isEmpty } from 'utils/collection-utils';
269 Examples of **correct** code for this rule:
274 /*eslint no-restricted-imports: ["error", "fs"]*/
276 import crypto from 'crypto';
277 export { foo } from "bar";
285 /*eslint no-restricted-imports: ["error", { "paths": ["fs"], "patterns": ["eslint/*"] }]*/
287 import crypto from 'crypto';
288 import eslint from 'eslint';
289 export * from "path";
297 /*eslint no-restricted-imports: ["error", { paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]*/
299 import DisallowedObject from "foo"
307 /*eslint no-restricted-imports: ["error", { paths: [{
309 importNames: ["DisallowedObject"],
310 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
313 import { AllowedObject as DisallowedObject } from "foo";
321 /*eslint no-restricted-imports: ["error", { patterns: [{
323 message: "Please use the default import from 'lodash' instead."
326 import lodash from 'lodash';
334 /*eslint no-restricted-imports: ["error", { patterns: [{
335 group: ["foo[A-Z]*"],
339 import pick from 'food';
347 /*eslint no-restricted-imports: ["error", { patterns: [{
349 importNames: ['isEmpty'],
350 message: "Use 'isEmpty' from lodash instead."
353 import { hasValues } from 'utils/collection-utils';
358 ## When Not To Use It
360 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.