Last updated: Mar 7, 2024
Reading time·3 min
import/prefer-default-export
rule globallyimport/prefer-default-export
rule for a single lineimport/prefer-default-export
rule for an entire fileThe ESLint "Prefer default export import/prefer-default-export" warning is
shown when you have a file that only contains a single export and use a named
export instead of a default
one.
To resolve the issue, disable the ESLint rule or use a default
export.
Here is an example of when the warning is shown.
// ⛔️ Prefer default export import/prefer-default-export export function greet(fullName) { return `hello ${fullName}`; }
The file only contains a single export and uses a named export instead of a default one.
One way to resolve the issue is to use a default
export.
// ✅ No warnings export default function greet(fullName) { return `hello ${fullName}`; }
Then you would import the function without curly braces.
import greet from './index.js' console.log(greet('bobby hadz'))
If you are exporting a const
declaration as default
, make sure to declare it
one line and export it on the next.
const site = 'bobbyhadz.com'; export default site;
Then you would import the default
export as follows.
import site from './index.js' console.log(site);
You can read more about using the ES6 modules import/export syntax in this article.
In short, the main differences between a default
and a named
export are:
default
export per file.named
exports in a file as necessary.default
exports are imported without curly braces.named
exports are imported with curly braces.default
export, you can use a different name.named
exports and imports.However, in some cases, you might want to disable the ESLint rule and use
named
exports.
import/prefer-default-export
rule globallyYou can edit your .eslintrc.js
file if you want to disable the
import/prefer-default-export
rule globally.
module.exports = { rules: { 'import/prefer-default-export': 'off', }, };
If you write your ESLint config using JSON, you have to edit your .eslintrc
or
.eslintrc.json
file.
{ "rules": { "import/prefer-default-export": "off" } }
Make sure to double-quote the keys and values and remove the trailing comma
after the last property in the rules
object.
import/prefer-default-export
rule for a single lineYou can use a comment if you only want to disable the
import/prefer-default-export
rule for a single line.
// eslint-disable-next-line import/prefer-default-export export const site = 'bobbyhadz.com';
Make sure the comment is placed above the line that causes the warning.
import/prefer-default-export
rule for an entire fileIf you want to disable the import/prefer-default-export
rule for an entire
file, add the following comment at the top of your file.
/* eslint-disable import/prefer-default-export */ export const site = 'bobbyhadz.com';
Make sure the comment is added at the top of the file in which the warning is raised or at least above the named export.
You can learn more about the related topics by checking out the following tutorials:
await
inside a loop.eslint no-await-in-loop