1 # Disallow specific imports (no-restricted-imports)
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`.
5 Why would you want to restrict imports?
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.
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.
13 This rule allows you to specify imports that you don't want to use in your application.
15 It applies to static imports only, not dynamic ones.
19 The syntax to specify restricted imports looks like this:
22 "no-restricted-imports": ["error", "import1", "import2"]
28 "no-restricted-imports": ["error", { "paths": ["import1", "import2"] }]
31 When using the object form, you can also specify an array of gitignore-style patterns:
34 "no-restricted-imports": ["error", {
35 "paths": ["import1", "import2"],
36 "patterns": ["import1/private/*", "import2/*", "!import2/good"]
40 You may also specify a custom message for any paths you want to restrict as follows:
43 "no-restricted-imports": ["error", {
45 "message": "Please use import-bar instead."
48 "message": "Please use import-quux instead."
55 "no-restricted-imports": ["error", {
58 "message": "Please use import-bar instead."
61 "message": "Please use import-quux instead."
66 or like this if you need to restrict only certain imports from a module:
69 "no-restricted-imports": ["error", {
72 "importNames": ["Bar"],
73 "message": "Please use Bar from /import-bar/baz/ instead."
78 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 import may match more than one pattern.
80 To restrict the use of all Node.js core imports (via https://github.com/nodejs/node/tree/master/lib):
83 "no-restricted-imports": ["error",
84 "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"
90 Examples of **incorrect** code for this rule:
93 /*eslint no-restricted-imports: ["error", "fs"]*/
99 /*eslint no-restricted-imports: ["error", "fs"]*/
101 export { fs } from 'fs';
105 /*eslint no-restricted-imports: ["error", "fs"]*/
111 /*eslint no-restricted-imports: ["error", { "paths": ["cluster"] }]*/
113 import cluster from 'cluster';
117 /*eslint no-restricted-imports: ["error", { "patterns": ["lodash/*"] }]*/
119 import pick from 'lodash/pick';
123 /*eslint no-restricted-imports: ["error", { paths: [{
125 importNames: ["default"],
126 message: "Please use the default import from '/bar/baz/' instead."
129 import DisallowedObject from "foo";
133 /*eslint no-restricted-imports: ["error", { paths: [{
135 importNames: ["DisallowedObject"],
136 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
139 import { DisallowedObject as AllowedObject } from "foo";
143 /*eslint no-restricted-imports: ["error", { paths: [{
145 importNames: ["DisallowedObject"],
146 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
149 import * as Foo from "foo";
152 Examples of **correct** code for this rule:
155 /*eslint no-restricted-imports: ["error", "fs"]*/
157 import crypto from 'crypto';
158 export { foo } from "bar";
162 /*eslint no-restricted-imports: ["error", { "paths": ["fs"], "patterns": ["eslint/*"] }]*/
164 import crypto from 'crypto';
165 import eslint from 'eslint';
166 export * from "path";
170 /*eslint no-restricted-imports: ["error", { paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]*/
172 import DisallowedObject from "foo"
176 /*eslint no-restricted-imports: ["error", { paths: [{
178 importNames: ["DisallowedObject"],
179 message: "Please import 'DisallowedObject' from '/bar/baz/' instead."
182 import { AllowedObject as DisallowedObject } from "foo";
185 ## When Not To Use It
187 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.