oxc-project/oxc · warning
Prefer default export on a file with single export.
Error message
Prefer default export on a file with single export.
What it means
This is oxlint's 'import/prefer-default-export' diagnostic in its default 'single' target mode. When a module contains exactly one named export, the rule asks you to make that export the default export instead. It mirrors the ESLint rule of the same name and exists because a lone named export is usually the module's main purpose, and default import syntax makes consumers shorter and refactoring simpler.
Source
Thrown at crates/oxc_linter/src/rules/import/prefer_default_export.rs:17
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{
context::LintContext,
module_record::ExportEntry,
rule::{DefaultRuleConfig, Rule},
};
fn prefer_default_export_diagnostic(span: Span, target: Target) -> OxcDiagnostic {
let msg = if target == Target::Single {
"Prefer default export on a file with single export."
} else {
"Prefer default export to be present on every file that has export."
};
OxcDiagnostic::warn(msg).with_help("Prefer a default export").with_label(span)
}
#[derive(Debug, Default, PartialEq, Clone, Copy, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "kebab-case")]
enum Target {
/// Prefer default export when there is only one export in the module.
#[default]
Single,
/// Prefer default export in any module that has exports.
Any,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct PreferDefaultExport {View on GitHub (pinned to e1e7af627c)
Solutions
- Convert the single named export to a default export: change 'export const x = ...' to 'const x = ...; export default x;' and update importers to 'import x from ...'.
- If more exports are planned, add them so the file no longer has exactly one export.
- If your style guide prefers named exports everywhere, switch the rule to target "any" is not the fix here — instead disable the rule or override it to "off" in .oxlintrc.json.
- Suppress one-off cases with // oxlint-disable-next-line import/prefer-default-export.
Example fix
// before (theme.ts)
export const theme = { primary: 'blue' };
// after (theme.ts)
const theme = { primary: 'blue' };
export default theme;
// consumers: import theme from './theme'; Defensive patterns
Strategy: validation
Validate before calling
// quick script: list files with exactly one non-default export
// run before enabling import/prefer-default-export
const { execSync } = require('node:child_process');
console.log(execSync("rg -l '^export (const|function|class) ' src/", { encoding: 'utf8' })); Prevention
- Agree on default vs named export convention per project before enabling this rule.
- During refactors that leave a single export, remember the rule will fire; convert it to a default export.
- Document the convention in CONTRIBUTING so new files comply from the start.
When it happens
Trigger: Enable import/prefer-default-export with target "single" (the default) and lint a module whose module record contains exactly one export entry that is not a default export, e.g. export const theme = ... alone in a file.
Common situations: Style-guide-driven codebases (airbnb-style configs enable this rule) flag utility files, constants files, and single-component files that grew from snippets. The warning appears the moment a second export is removed during refactoring, leaving one survivor.
Related errors
- Prefer default export to be present on every file that has e
- Exporting mutable '{kind_str}' binding, use 'const' instead.
- Tried to get an allocator from an empty `FixedSizeAllocatorP
- Prefer using inline type specifiers instead of a top-level t
- Prefer using a top-level type-only import instead of inline
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/b70a1769906d2f0b.
Report an issue: GitHub.