import/no-anonymous-default-export
Disallow default exports that have no name
Also called import-x/no-anonymous-default-export
You can't search for a default export that has no name, and each importing file makes up its own name for it. An anonymous function or class also shows up without a useful name in stack traces and dev tools. Give the value a name first, then export it.
Functions, classes, arrow functions, objects, arrays, literals and new expressions are reported. The result of a call, as in export default defineConfig({}), is allowed by default.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"import/no-anonymous-default-export" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"import/no-anonymous-default-export" = ["error", {
allowAnonymousFunction = false,
allowAnonymousClass = false,
allowArrowFunction = false,
allowObject = false,
allowArray = false,
allowLiteral = false,
allowNew = false,
allowCallExpression = true,
}]Examples#
example.js1 problem
export default function () {Unexpected default export of anonymous function
return "ok";
}example.js1 problem
export default items => items.length;Assign arrow function to a variable before exporting as module defaultexample.js1 problem
export default { port: 3000 };Assign object to a variable before exporting as module defaultexample.jsNo problems
export default function status() {
return "ok";
}example.jsNo problems
const config = { port: 3000 };
export default config;example.jsNo problems
export default defineConfig({ port: 3000 });Options#
| Option | Default | What it allows |
|---|---|---|
allowAnonymousFunction | false | export default function () {} |
allowAnonymousClass | false | export default class {} |
allowArrowFunction | false | export default () => {} |
allowObject | false | export default { port: 3000 } |
allowArray | false | export default [1, 2] |
allowLiteral | false | export default 42, strings, template literals, regular expressions, true, null |
allowNew | false | export default new Server() |
allowCallExpression | true | export default createServer() |
example.jswith
{ "allowObject": true }No problemsexport default { port: 3000 };example.jswith
{ "allowCallExpression": false }1 problemexport default defineConfig({ port: 3000 });Assign call result to a variable before exporting as module default