Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force anonymous default export?

If you have a file named same as default export, why do you need to name this export? I am not DRY there.

We have a rule to prevent the anonymous default export, but how can I do the opposite and force an error when somebody is not using an anonymous export?

like image 639
T_DaMER Avatar asked Oct 15 '25 14:10

T_DaMER


1 Answers

If you have a file named same as default export, why do you need to name this export?

You don't. That's why you are linking to a linter rule - it's not required, but considered "best practice" by some, because

Ensuring that default exports are named helps improve the grepability of the codebase by encouraging the re-use of the same identifier for the module's default export at its declaration site and at its import sites.

Basically, people think that if you have

export default () => { console.log('Hello'); }

people are more likely to do

import logHello from 'sayHello'; // file one
import greeting from 'sayHello'; // file two
import sayHello from 'sayHello'; // file three

but that if you do

const sayHello = () => {
    console.log('Hello');
}
export default sayHello

people will more consistently do

import sayHello from 'sayHello';

which makes the code easier to read, because the same function is called the same thing no matter where you import it from.

When you ask

how can I force it?

I am assuming you mean how can you force the linter to accept your anonymous default export? You would either need to inline disable the rule:

/* eslint import/no-anonymous-default-export: [2, {"allowArrowFunction": true}] */
export default () => {}

or update your eslint config to accept it in general.

like image 95
dave Avatar answered Oct 17 '25 04:10

dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!