oxc-project/oxc · error

Do not assign to imported bindings

Error message

Do not assign to imported bindings

What it means

Diagnostic from oxlint's no-import-assign rule (ESLint port, eslint:recommended). Imported bindings are read-only live bindings in ES modules; writing to them fails at runtime (TypeError in browsers, SyntaxError in Node for plain assignment). The rule statically flags every write to an import binding: plain and compound assignment, increment/decrement, destructuring assignment targets, for-of targets, and deletes or writes on namespace members.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_import_assign.rs:14

use oxc_ast::{
    AstKind,
    ast::{AssignmentTarget, AssignmentTargetMaybeDefault, Expression, ImportDeclarationSpecifier},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, NodeId, Reference};
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;

use crate::{context::LintContext, rule::Rule};

fn no_import_assign_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Do not assign to imported bindings")
        .with_help("Imported bindings are readonly")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoImportAssign;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow assigning to imported bindings.
    ///
    /// ### Why is this bad?
    ///
    /// The updates of imported bindings by ES Modules cause runtime errors.
    ///
    /// The TypeScript compiler generally enforces this check already. Although
    /// it should be noted that there are some cases TypeScript does not catch, such

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Assign to a fresh local copy: let local = a; local = 1;.
  2. For mutable shared state, export a setter function or a mutable object from the module.
  3. In tests, use the loader's mocking API (vi.mock / jest.mock) instead of assignment.
  4. Remove the write entirely; imported bindings are a read-only contract.

Example fix

// before
import { count } from './store.js';
count = 10;

// after
import { setCount } from './store.js';
setCount(10);
Defensive patterns

Strategy: validation

Validate before calling

npx oxlint -A all -D no-import-assign src/

Prevention

When it happens

Trigger: import { a } from 'm'; a = 1;; import def from 'm'; def += 1; or ++def;; [a] = pairs; where a is imported; for (a of list);; import * as ns from 'm'; ns.foo = 1; or delete ns.foo;

Common situations: Trying to mock or override an imported value in tests; attempts to reconfigure imported constants; cyclic-module workarounds that assign into imports to break loops.

Related errors


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