]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-duplicate-imports.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-duplicate-imports.md
1 ---
2 title: no-duplicate-imports
3 layout: doc
4 rule_type: problem
5 ---
6
7
8 Using a single `import` statement per module will make the code clearer because you can see everything being imported from that module on one line.
9
10 In the following example the `module` import on line 1 is repeated on line 3. These can be combined to make the list of imports more succinct.
11
12 ```js
13 import { merge } from 'module';
14 import something from 'another-module';
15 import { find } from 'module';
16 ```
17
18 ## Rule Details
19
20 This rule requires that all imports from a single module that can be merged exist in a single `import` statement.
21
22 Example of **incorrect** code for this rule:
23
24 ::: incorrect
25
26 ```js
27 /*eslint no-duplicate-imports: "error"*/
28
29 import { merge } from 'module';
30 import something from 'another-module';
31 import { find } from 'module';
32 ```
33
34 :::
35
36 Example of **correct** code for this rule:
37
38 ::: correct
39
40 ```js
41 /*eslint no-duplicate-imports: "error"*/
42
43 import { merge, find } from 'module';
44 import something from 'another-module';
45 ```
46
47 :::
48
49 Example of **correct** code for this rule:
50
51 ::: correct
52
53 ```js
54 /*eslint no-duplicate-imports: "error"*/
55
56 // not mergeable
57 import { merge } from 'module';
58 import * as something from 'module';
59 ```
60
61 :::
62
63 ## Options
64
65 This rule takes one optional argument, an object with a single key, `includeExports` which is a `boolean`. It defaults to `false`.
66
67 If re-exporting from an imported module, you should add the imports to the `import`-statement, and export that directly, not use `export ... from`.
68
69 Example of **incorrect** code for this rule with the `{ "includeExports": true }` option:
70
71 ::: incorrect
72
73 ```js
74 /*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/
75
76 import { merge } from 'module';
77
78 export { find } from 'module';
79 ```
80
81 :::
82
83 Example of **correct** code for this rule with the `{ "includeExports": true }` option:
84
85 ::: correct
86
87 ```js
88 /*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/
89
90 import { merge, find } from 'module';
91
92 export { find };
93 ```
94
95 :::
96
97 Example of **correct** code for this rule with the `{ "includeExports": true }` option:
98
99 ::: correct
100
101 ```js
102 /*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/
103
104 import { merge, find } from 'module';
105
106 // cannot be merged with the above import
107 export * as something from 'module';
108
109 // cannot be written differently
110 export * from 'module';
111 ```
112
113 :::