prefer-default-export
Added in v0.9.4Configuration
Rule Details
Prefer a default export when a file exports a single name. With
target: 'any', require a default export whenever the file has named exports.
Examples of incorrect code with the default options:
Examples of correct code with the default options:
A default export, a star export (including export * as name), or an exported
type alias or interface exempts the entire file. Star exports do not inspect
the other module. Type-only export lists such as export type { Value } are
counted as named exports, unlike export type Value = number.
The rule reports the last named export when the file does not meet the selected policy. It provides no automatic fixes or suggestions. Files without ES module exports, including files using only CommonJS assignments, are allowed.
Options
The rule accepts an object with a target property:
"single"(default): require a default export when there is exactly one named export."any": require a default export when there is at least one named export. The file exemptions above still apply.
For example, with ["error", { "target": "any" }], this code is incorrect:
This code is correct with target: 'any':
When Not To Use It
Disable this rule if your project prefers named exports even when a file has only one export.
Differences from upstream
Compared with eslint-plugin-import v2.32.0:
- Babel re-export shortcuts are unsupported. Replace
export default from './values.js'withexport { default } from './values.js', which satisfies eithertarget. Replaceexport value, { other } from './values.js'withexport { default as value, other } from './values.js'. This exports two named values, so it satisfiestarget: 'single'but still needs a default export withtarget: 'any', unless another export exempts the file.