{"record":{"id":"90f4a9452c2c216c","repo":"oxc-project/oxc","slug":"ts1202","errorCode":"TS1202","errorMessage":"Import assignment cannot be used when targeting ECMAScript modules.","messagePattern":"Import assignment cannot be used when targeting ECMAScript modules\\.","errorType":"error_code","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_transformer/src/typescript/diagnostics.rs","lineNumber":6,"sourceCode":"use oxc_diagnostics::OxcDiagnostic;\nuse oxc_span::Span;\n\n#[cold]\npub fn import_equals_cannot_be_used_in_esm(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Import assignment cannot be used when targeting ECMAScript modules.\")\n        .with_help(\n            \"Consider using 'import * as ns from \\\"mod\\\"',\n         'import {a} from \\\"mod\\\"', 'import d from \\\"mod\\\"', or another module format instead.\",\n        )\n        .with_label(span)\n        .with_error_code(\"TS\", \"1202\")\n}\n\n#[cold]\npub fn export_assignment_cannot_bed_used_in_esm(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Export assignment cannot be used when targeting ECMAScript modules.\")\n        .with_help(\"Consider using 'export default' or another module format instead.\")\n        .with_label(span)\n        .with_error_code(\"TS\", \"1203\")\n}\n\n#[cold]\npub fn ambient_module_nested(span: Span) -> OxcDiagnostic {","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_transformer/src/typescript/diagnostics.rs#L1-L24","documentation":"This is Oxc's port of TypeScript error TS1202: `import x = require(\"...\")` (TSImportEqualsDeclaration with an ExternalModuleReference) is a CommonJS-style import assignment and cannot be emitted when the output module format is ECMAScript modules. The TypeScript module transform lowers it to `const x = require(\"...\")` and reports the diagnostic when `self.module.is_esm()` at crates/oxc_transformer/src/typescript/module.rs:157-159. Note `Module::is_esm()` (options/module.rs:28) is true only for `Module::Esm`; the default `Module::Preserve` does not trigger it.","triggerScenarios":"Configuring oxc-transform with `module: Esm` (or a Babel config mapping to ESM output) and transforming TypeScript containing `import fs = require('node:fs');` where the import has value references (type-only usages are stripped earlier). Both classic `import x = require('m')` forms and re-exported ones (`export import x = require('m')` reaches the same path via enter_declaration) can trip it.","commonSituations":"Migrating a legacy TS codebase (originally `module: commonjs`, full of `import x = require(...)` for cycles or lazy requires) to ESM output (`module: esnext`/`preserve`+ESM bundler); Node dual-mode packages where CJS-style TS is reused in an ESM build; copying wiring/ORM snippets written for old Electron or Vue 2 toolchains into an ESM-targeted oxc pipeline.","solutions":["Replace with ESM syntax: `import * as ns from \"mod\"`, `import { a } from \"mod\"`, or default-import `import d from \"mod\"` (the help text in diagnostics.rs lists exactly these)","If the import exists only to defer/lazy-load, use dynamic import: `const mod = await import(\"mod\")` or `import(\"mod\").then(...)`","If the file must stay CommonJS-style, set the transform's module option to `commonjs` so `module.is_esm()` is false (note: `Preserve`, the default, also does not error)","For pure type aliases to modules, mark it type-only (`import type x = require('m')`) or ensure all references are types so it is removed instead of lowered"],"exampleFix":"// before\nimport utils = require('./utils');\nexport = doThing(utils);\n\n// after\nimport * as utils from './utils';\nexport default doThing(utils);","handlingStrategy":"validation","validationCode":"// Before running the ESM-output transform, detect import-equals in sources\nfunction hasImportEquals(src: string): boolean {\n  return /^\\s*(export\\s+)?import\\s+\\w+\\s*=\\s*require\\s*\\(/m.test(src);\n}\n// Grounded decision: only warn when output module is Esm\nconst moduleIsEsm = transformOptions.module === 'esm';\nif (moduleIsEsm && hasImportEquals(src)) failBuild('rewrite import x = require(...)');","typeGuard":"// Static guard for a TS codebase: ban the syntax via config instead of runtime\ntsconfig: { \"module\": \"esnext\", \"verbatimModuleSyntax\": true } // makes tsc flag import = for ESM","tryCatchPattern":"// oxc collects diagnostics rather than throwing; gate your pipeline on the TS code\nlet result = transformer.build(source);\nresult.errors.iter().filter(|d| d.code() == Some((\"TS\", \"1202\"))).for_each(|d| {\n    route_to_esm_migration_todo(d);\n});","preventionTips":["Decide one module format per package and forbid the other's syntax via lint rules (`no-require-imports`, `@typescript-eslint/consistent-type-imports`)","When migrating CJS-style TS to ESM, run a codemod converting `import x = require('m')` to `import` forms before flipping the module option","Keep the transform's module option explicit (`esm` or `commonjs`) instead of relying on defaults so this class of mismatch surfaces deterministically","For type-only module aliases use `import type` so they are stripped rather than lowered to require()"],"tags":["oxc","typescript","transformer","esm","commonjs","ts1202"],"backgroundTag":"commonjs-esm-interop","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}