{"record":{"id":"7a3a41bea7cdfd12","repo":"oxc-project/oxc","slug":"ts1203","errorCode":"TS1203","errorMessage":"Export assignment cannot be used when targeting ECMAScript modules.","messagePattern":"Export 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":17,"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 {\n    OxcDiagnostic::warn(\"Ambient modules cannot be nested in other modules or namespaces.\")\n        .with_label(span)\n}\n\n#[cold]\npub fn namespace_exporting_non_const(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Namespaces exporting non-const are not supported by Oxc. Change to const or see: https://oxc.rs/docs/guide/usage/transformer/typescript.html#partial-namespace-support\")\n        .with_label(span)\n}\n\n#[cold]","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_transformer/src/typescript/diagnostics.rs#L1-L35","documentation":"Oxc's port of TypeScript error TS1203: `export = <expression>` (TSExportAssignment, the CommonJS export-assignment form) cannot be emitted when the output module format is ESM. The transform rewrites it to `module.exports = <expression>` at crates/oxc_transformer/src/typescript/module.rs:59-92 and reports the diagnostic when `self.module.is_esm()` (module.rs:65-69). As with TS1202, this fires only when `Module::Esm` is explicitly configured — the default `Module::Preserve` and `Module::CommonJS` do not warn.","triggerScenarios":"Configuring oxc-transform with `module: Esm` and transforming a TypeScript file containing `export = someObjectOrFunction;` (enter_statement → transform_ts_export_assignment at module.rs:42-46). The statement is still lowered to `module.exports = ...`, so the emitted ESM file would call a nonexistent `module` binding unless a bundler cleans it up.","commonSituations":"Legacy `.d.ts`-adjacent implementation files and old Node library code written with `export =` for exact CJS shape control, later fed into an ESM-first build (Vite/Rollup-style config with oxc); migrating packages from `module: commonjs` tsconfig to ESM without rewriting export style; generated code from older OpenAPI/gRPC toolchains that emits `export =` clients.","solutions":["Replace `export = expr` with `export default expr` (the diagnostic's own help text)","If consumers rely on the CJS namespace shape (`.default` interop), keep default export and let the bundler's interop handle it, or publish proper CJS by setting module to `commonjs`","For object-shape exports, prefer named exports: `export const a = ...; export const b = ...;` instead of assigning one bag object"],"exampleFix":"// before\nfunction createServer() { ... }\nexport = createServer;\n\n// after\nfunction createServer() { ... }\nexport default createServer;","handlingStrategy":"validation","validationCode":"// Detect export-assignment before ESM transform\nfunction hasExportAssignment(src: string): boolean {\n  return /^\\s*export\\s*=\\s*[^=]/m.test(src);\n}\nconst moduleIsEsm = transformOptions.module === 'esm';\nif (moduleIsEsm && hasExportAssignment(src)) {\n  throw new Error('export = cannot be emitted as ESM; use export default');\n}","typeGuard":"// Repo policy guard: fail CI on any CJS-style export syntax in ESM packages\nfunction assertNoCjsExports(files: string[]) {\n  for (const f of files.filter(f => f.endsWith('.ts'))) {\n    if (/^\\s*export\\s*=/m.test(read(f))) throw new Error(`${f}: use export default / named exports`);\n  }\n}","tryCatchPattern":"// Gate on the TS code after transform, mirroring tsc\nfor (const d of result.errors) {\n  if (d.code === 'TS1203') queueForEsmRewrite(d.file, d.span);\n}","preventionTips":["Adopt `export default` / named exports in all files destined for ESM output","When absorbing legacy packages, convert `export =` in the same commit that switches the build to ESM","Run tsc with `module: esnext` over the source as a cross-check; TS1203 there catches the same mismatch early"],"tags":["oxc","typescript","transformer","esm","commonjs","ts1203"],"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-14T05:17:10.506Z"}