11 The import statement is used to import members (functions, objects or primitives) that have been exported from an external module. Using a specific member syntax:
14 // single - Import single member.
15 import myMember from "my-module.js";
16 import {myOtherMember} from "my-other-module.js";
18 // multiple - Import multiple members.
19 import {foo, bar} from "my-module.js";
21 // all - Import all members, where myModule contains all the exported bindings.
22 import * as myModule from "my-module.js";
25 The import statement can also import a module without exported bindings. Used when the module does not export anything, but runs it own code or changes the global context object.
28 // none - Import module without exported bindings.
32 When declaring multiple imports, a sorted list of import declarations make it easier for developers to read the code and find necessary imports later. This rule is purely a matter of style.
36 This rule checks all import declarations and verifies that all imports are first sorted by the used member syntax and then alphabetically by the first member or alias name.
38 The `--fix` option on the command line automatically fixes some problems reported by this rule: multiple members on a single line are automatically sorted (e.g. `import { b, a } from 'foo.js'` is corrected to `import { a, b } from 'foo.js'`), but multiple lines are not reordered.
42 This rule accepts an object with its properties as
44 * `ignoreCase` (default: `false`)
45 * `ignoreDeclarationSort` (default: `false`)
46 * `ignoreMemberSort` (default: `false`)
47 * `memberSyntaxSortOrder` (default: `["none", "all", "multiple", "single"]`); all 4 items must be present in the array, but you can change the order:
48 * `none` = import module without exported bindings.
49 * `all` = import all members provided by exported bindings.
50 * `multiple` = import multiple members.
51 * `single` = import single member.
52 * `allowSeparatedGroups` (default: `false`)
54 Default option settings are:
58 "sort-imports": ["error", {
60 "ignoreDeclarationSort": false,
61 "ignoreMemberSort": false,
62 "memberSyntaxSortOrder": ["none", "all", "multiple", "single"],
63 "allowSeparatedGroups": false
72 Examples of **correct** code for this rule when using default options:
77 /*eslint sort-imports: "error"*/
78 import 'module-without-export.js';
79 import * as bar from 'bar.js';
80 import * as foo from 'foo.js';
81 import {alpha, beta} from 'alpha.js';
82 import {delta, gamma} from 'delta.js';
83 import a from 'baz.js';
84 import {b} from 'qux.js';
86 /*eslint sort-imports: "error"*/
87 import a from 'foo.js';
88 import b from 'bar.js';
89 import c from 'baz.js';
91 /*eslint sort-imports: "error"*/
93 import * as bar from 'bar.js';
94 import {a, b} from 'baz.js';
95 import c from 'qux.js';
96 import {d} from 'quux.js';
98 /*eslint sort-imports: "error"*/
99 import {a, b, c} from 'foo.js'
104 Examples of **incorrect** code for this rule when using default options:
109 /*eslint sort-imports: "error"*/
110 import b from 'foo.js';
111 import a from 'bar.js';
113 /*eslint sort-imports: "error"*/
114 import a from 'foo.js';
115 import A from 'bar.js';
117 /*eslint sort-imports: "error"*/
118 import {b, c} from 'foo.js';
119 import {a, b} from 'bar.js';
121 /*eslint sort-imports: "error"*/
122 import a from 'foo.js';
123 import {b, c} from 'bar.js';
125 /*eslint sort-imports: "error"*/
126 import {a} from 'foo.js';
127 import {b, c} from 'bar.js';
129 /*eslint sort-imports: "error"*/
130 import a from 'foo.js';
131 import * as b from 'bar.js';
133 /*eslint sort-imports: "error"*/
134 import {b, a, c} from 'foo.js'
141 When `true` the rule ignores the case-sensitivity of the imports local name.
143 Examples of **incorrect** code for this rule with the `{ "ignoreCase": true }` option:
148 /*eslint sort-imports: ["error", { "ignoreCase": true }]*/
150 import B from 'foo.js';
151 import a from 'bar.js';
156 Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option:
161 /*eslint sort-imports: ["error", { "ignoreCase": true }]*/
163 import a from 'foo.js';
164 import B from 'bar.js';
165 import c from 'baz.js';
172 ### `ignoreDeclarationSort`
174 Ignores the sorting of import declaration statements.
176 Examples of **incorrect** code for this rule with the default `{ "ignoreDeclarationSort": false }` option:
181 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": false }]*/
182 import b from 'foo.js'
183 import a from 'bar.js'
188 Examples of **correct** code for this rule with the `{ "ignoreDeclarationSort": true }` option:
193 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
194 import a from 'foo.js'
195 import b from 'bar.js'
203 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
204 import b from 'foo.js'
205 import a from 'bar.js'
212 ### `ignoreMemberSort`
214 Ignores the member sorting within a `multiple` member import declaration.
216 Examples of **incorrect** code for this rule with the default `{ "ignoreMemberSort": false }` option:
221 /*eslint sort-imports: ["error", { "ignoreMemberSort": false }]*/
222 import {b, a, c} from 'foo.js'
227 Examples of **correct** code for this rule with the `{ "ignoreMemberSort": true }` option:
232 /*eslint sort-imports: ["error", { "ignoreMemberSort": true }]*/
233 import {b, a, c} from 'foo.js'
240 ### `memberSyntaxSortOrder`
242 There are four different styles and the default member syntax sort order is:
244 * `none` - import module without exported bindings.
245 * `all` - import all members provided by exported bindings.
246 * `multiple` - import multiple members.
247 * `single` - import single member.
249 All four options must be specified in the array, but you can customize their order.
251 Examples of **incorrect** code for this rule with the default `{ "memberSyntaxSortOrder": ["none", "all", "multiple", "single"] }` option:
256 /*eslint sort-imports: "error"*/
257 import a from 'foo.js';
258 import * as b from 'bar.js';
263 Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }` option:
268 /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }]*/
270 import a from 'foo.js';
271 import * as b from 'bar.js';
276 Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }` option:
281 /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }]*/
283 import * as foo from 'foo.js';
284 import z from 'zoo.js';
285 import {a, b} from 'foo.js';
290 Default is `["none", "all", "multiple", "single"]`.
292 ### `allowSeparatedGroups`
294 When `true` the rule checks the sorting of import declaration statements only for those that appear on consecutive lines.
296 In other words, a blank line or a comment line or line with any other statement after an import declaration statement will reset the sorting of import declaration statements.
298 Examples of **incorrect** code for this rule with the `{ "allowSeparatedGroups": true }` option:
303 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
305 import b from 'foo.js';
306 import c from 'bar.js';
307 import a from 'baz.js';
312 Examples of **correct** code for this rule with the `{ "allowSeparatedGroups": true }` option:
317 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
319 import b from 'foo.js';
320 import c from 'bar.js';
322 import a from 'baz.js';
330 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
332 import b from 'foo.js';
333 import c from 'bar.js';
335 import a from 'baz.js';
343 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
345 import b from 'foo.js';
346 import c from 'bar.js';
348 import a from 'baz.js';
355 ## When Not To Use It
357 This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing imports isn't a part of your coding standards, then you can leave this rule disabled.