oxc-project/oxc · warning · OxcDiagnostic

Assign literal to a variable before exporting as module defa

Error message

Assign literal to a variable before exporting as module default

What it means

Diagnostic from the oxlint rule import/no-anonymous-default-export (style category). It fires for the catch-all branch: the default export is a literal. Because the code checks `expr.is_literal() || matches!(expr, Expression::TemplateLiteral(_))`, this covers numbers, strings, booleans, null, BigInt and regex literals, and ALL template literals — even ones with substitutions like ``export default `${name}```. `allowLiteral` defaults to false.

Source

Thrown at crates/oxc_linter/src/rules/import/no_anonymous_default_export.rs:191

                ctx.diagnostic(no_anonymous_default_export_diagnostic(
                    export_decl.span,
                    "Assign instance to a variable before exporting as module default",
                ));
            }
            ExportDefaultDeclarationKind::ArrayExpression(_) if !self.allow_array => {
                ctx.diagnostic(no_anonymous_default_export_diagnostic(
                    export_decl.span,
                    "Assign array to a variable before exporting as module default",
                ));
            }
            _ => {
                if let Some(expr) = export_decl.declaration.as_expression()
                    && !self.allow_literal
                    && (expr.is_literal() || matches!(expr, Expression::TemplateLiteral(_)))
                {
                    ctx.diagnostic(no_anonymous_default_export_diagnostic(
                        export_decl.span,
                        "Assign literal to a variable before exporting as module default",
                    ));
                }
            }
        }
    }
}

#[test]
fn test() {
    use crate::tester::Tester;
    use serde_json::json;

    let pass = vec![
        ("export default function foo() {}", None),
        ("export default class MyClass {}", None),
        ("const foo = 123; export default foo", None),
        ("export default foo(bar)", None),
        ("export default []", Some(json!([{ "allowArray": true }]))),

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Assign the literal to a named constant, then export it: `const VERSION = 42; export default VERSION;`
  2. Enable `["warn", { "allowLiteral": true }]` if literal defaults are acceptable in your codebase
  3. Add a targeted disable comment for intentional one-offs (e.g. generated files)

Example fix

// before
export default `v1.2.3`;

// after
const VERSION = `v1.2.3`;
export default VERSION;
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{ "rules": { "import/no-anonymous-default-export": ["warn", { "allowLiteral": true }] } }

Prevention

When it happens

Trigger: `export default 42`, `export default "foo"`, `export default true`, `export default null`, `export default /^123/`, and any `export default \`...\`` (template literals with or without placeholders) when the rule is on and `allowLiteral` is false. Emitted from the `_ =>` fallback arm in crates/oxc_linter/src/rules/import/no_anonymous_default_export.rs:191.

Common situations: Marker/config modules ("export default 42" version constants, regex patterns); code migrated from projects that allowed literal defaults; note that developers are often surprised the template-literal case is flagged even when static.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/d32992cda8598d2c. Report an issue: GitHub.