oxc-project/oxc · warning · OxcDiagnostic

Assign instance to a variable before exporting as module def

Error message

Assign instance 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 when a module's default export is a `new` expression, e.g. `export default new Foo()`. The rule wants default exports bound to a named identifier so the same name is greppable at the declaration site and at import sites; `allowNew` defaults to false, so instance exports are flagged (note `allowCallExpression` defaults to true, so `export default foo()` is not flagged).

Source

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

                    "Assign arrow function to a variable before exporting as module default",
                ));
            }
            ExportDefaultDeclarationKind::ObjectExpression(_) if !self.allow_object => {
                ctx.diagnostic(no_anonymous_default_export_diagnostic(
                    export_decl.span,
                    "Assign object to a variable before exporting as module default",
                ));
            }
            ExportDefaultDeclarationKind::CallExpression(_) if !self.allow_call_expression => {
                ctx.diagnostic(no_anonymous_default_export_diagnostic(
                    export_decl.span,
                    "Assign call result to a variable before exporting as module default",
                ));
            }
            ExportDefaultDeclarationKind::NewExpression(_) if !self.allow_new => {
                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",
                    ));
                }

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Assign the instance to a named constant, then export the constant: `const client = new HttpClient(); export default client;`
  2. If anonymous instance exports are acceptable, enable the option: `["warn", { "allowNew": true }]` in the oxlint/eslint config
  3. Suppress for a one-off case with an `// oxlint-disable-next-line import/no-anonymous-default-export` comment (or the eslint-disable equivalent)

Example fix

// before
export default new HttpClient();

// after
const httpClient = new HttpClient();
export default httpClient;
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — decide the policy up front so CI is predictable
{ "rules": { "import/no-anonymous-default-export": ["warn", { "allowNew": true }] } }

Prevention

When it happens

Trigger: Any `export default new SomeClass(...)` in a file linted with this rule enabled and without the option `{ "allowNew": true }`. Emitted by the `ExportDefaultDeclarationKind::NewExpression(_) if !self.allow_new` arm in crates/oxc_linter/src/rules/import/no_anonymous_default_export.rs:175.

Common situations: Singleton-style modules such as `export default new HttpClient()` or `export default new ConfigStore()` written before the rule was adopted; teams migrating from eslint-plugin-import configs where they had toggled allowNew; enabling the whole import plugin category in .oxlintrc.json without reviewing per-rule options.

Related errors


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