oxc-project/oxc · warning

A `require()` style import is forbidden.

Error message

A `require()` style import is forbidden.

What it means

This is the CommonJS variant of oxlint's 'import/no-unassigned-imports' diagnostic. It fires when a call expression statement that is a require() call takes a string literal argument and its result is discarded, e.g. require('should'). The message exists because a require whose return value is unused signals either dead code or a hidden side-effect dependency, which hurts testability and bundle trimming.

Source

Thrown at crates/oxc_linter/src/rules/import/no_unassigned_import.rs:121

                        "Imported module should be assigned",
                    ));
                }
            }
            AstKind::ExpressionStatement(statement) => {
                let Expression::CallExpression(call_expr) = &statement.expression else {
                    return;
                };
                if !call_expr.is_require_call() {
                    return;
                }
                let first_arg = &call_expr.arguments[0];
                let Argument::StringLiteral(source_str) = first_arg else {
                    return;
                };
                if !self.is_match_allow_globs(source_str.value.as_str()) {
                    ctx.diagnostic(no_unassigned_import_diagnostic(
                        call_expr.span,
                        "A `require()` style import is forbidden.",
                    ));
                }
            }
            _ => {}
        }
    }
}

impl NoUnassignedImportConfig {
    fn is_match_allow_globs(&self, source: &str) -> bool {
        self.globs.iter().any(|glob| fast_glob::glob_match(glob.as_str(), source))
    }
}

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

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the require if the module's side effects are not needed.
  2. Capture the result if you actually use it: const should = require('should').
  3. Add the intentional side-effect modules to the allow globs: "import/no-unassigned-imports": ["error", { "allow": ["dotenv/config", "**/*.css"] }].
  4. Suppress the single occurrence with an // oxlint-disable-next-line import/no-unassigned-imports comment if the pattern is deliberate.

Example fix

// before
require('dotenv/config');
require('./polyfills');

// after
// .oxlintrc.json: "import/no-unassigned-imports": ["error", { "allow": ["dotenv/config"] }]
require('dotenv/config');
const polyfills = require('./polyfills');
Defensive patterns

Strategy: validation

Validate before calling

# list bare require() statements before enabling the rule
rg -n "^\s*require\s*\(\s*['\"][^'\"]+['\"]\s*\)\s*;" src/

Prevention

When it happens

Trigger: Run oxlint with import/no-unassigned-imports enabled on code containing an ExpressionStatement whose expression is a require() call with a string literal first argument that does not match any allow glob. The diagnostic is attached to the call expression's span.

Common situations: Classic Node scripts and migration-era codebases contain require('./env'), require('dotenv/config'), or require('should') purely for side effects. Enabling this rule without an allow list flags all of them at once, which looks like a regression but is the rule working as designed.

Understand the failure class

Related errors


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