I've been searching the web and StackOverflow for this for quite some time with no success.
What I'm trying to do have ESLint mark the following as errors:
export default ...;
with default
being the key here. So far the best I've got is a reference to eslint-plugin-import
plugin and some of its rules that can make me closer to the target, namely the no-anonymous-default-export rule. But even with this rule the following default exports would be valid:
const foo = 123
export default foo
export default class MyClass() {}
export default function foo() {}
How can I configure ESLint in such a way that these four would also be considered errors?
The "Module has no default export" error occurs when we try to import as default from a module that doesn't have a default export. To solve the error make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule' .
There are two primary ways to configure ESLint: Configuration Comments - use JavaScript comments to embed configuration information directly into a file. Configuration Files - use a JavaScript, JSON, or YAML file to specify configuration information for an entire directory and all of its subdirectories.
If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.
You can do this with the no-restricted-syntax
rule. Try pasting this in the demo to try it out (you'll need to change "Source Type" to "module" first in the options):
/* eslint "no-restricted-syntax": ["error", {
"selector": "ExportDefaultDeclaration",
"message": "Prefer named exports"
}] */
export default class Foo { } // 5:1 - Prefer named exports (no-restricted-syntax)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With