{"record":{"id":"02180d686baaf3d6","repo":"swc-project/swc","slug":"failed-to-parse-a-js-file-as-a-module","errorCode":null,"errorMessage":"failed to parse a js file as a module","messagePattern":"failed to parse a js file as a module","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/dbg-swc/src/util/mod.rs","lineNumber":69,"sourceCode":"\n    let mut errors = Vec::new();\n    let comments = SingleThreadedComments::default();\n    let res = parse_file_as_module(\n        &fm,\n        Syntax::Es(Default::default()),\n        EsVersion::latest(),\n        Some(&comments),\n        &mut errors,\n    )\n    .map_err(|err| HANDLER.with(|handler| err.into_diagnostic(handler).emit()));\n\n    for err in errors {\n        HANDLER.with(|handler| err.into_diagnostic(handler).emit());\n    }\n\n    let mut m = match res {\n        Ok(v) => v,\n        Err(()) => bail!(\"failed to parse a js file as a module\"),\n    };\n\n    m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));\n\n    Ok(ModuleRecord {\n        module: m,\n        comments,\n        top_level_mark,\n        unresolved_mark,\n    })\n}\n\npub fn print_js(cm: Arc<SourceMap>, m: &Module, minify: bool) -> Result<String> {\n    let mut buf = Vec::new();\n\n    {\n        let mut wr = Box::new(JsWriter::new(cm.clone(), \"\\n\", &mut buf, None)) as Box<dyn WriteJs>;\n        if minify {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/dbg-swc/src/util/mod.rs#L51-L87","documentation":"dbg-swc's shared parse_js helper parses input strictly as an ECMAScript module (parse_file_as_module with default EsSyntax and no TS config); a fatal parse error first emits its diagnostic through the swc handler, then this generic bail fires. The emitted diagnostic carries the real line/column and message. Because this helper has no TypeScript configuration, .ts input fails here.","triggerScenarios":"Feeding any dbg-swc subcommand that uses parse_js a file that is not valid plain ESM: TypeScript syntax, Flow annotations, JSON, HTML, or truncated JS from a reduction run.","commonSituations":"Pointing the tool at .ts/.flow files by accident; creduce-mutated candidates that no longer parse (where this is the expected 'not interesting' outcome); files with BOM/encoding corruption.","solutions":["Read the diagnostic emitted just before the bail for the exact syntax error position","Point the tool at plain JavaScript, or strip TS/Flow types first with the appropriate pass","During reduction sessions, treat this as 'candidate is invalid' rather than a tool bug","Check the file encoding if the diagnostic points at column 0 of line 1"],"exampleFix":"# before\n$ dbg-swc es minifier ... --file ./src/app.ts\nerror: failed to parse a js file as a module\n\n# after - strip types first, then run on plain JS\n$ npx swc cli-strip ./src/app.ts -o ./out/app.js\n$ dbg-swc es minifier ... --file ./out/app.js","handlingStrategy":"try-catch","validationCode":"fn looks_like_plain_js(path: &Path) -> bool {\n    matches!(\n        path.extension().and_then(|e| e.to_str()),\n        Some(\"js\") | Some(\"jsx\") | Some(\"mjs\") | Some(\"cjs\")\n    )\n}\n\n// dbg-swc's parse_js has no TS config - reject .ts/.tsx/.flow inputs up front\nassert!(looks_like_plain_js(&input), \"parse_js accepts plain ESM JavaScript only\");","typeGuard":null,"tryCatchPattern":"match parse_js(fm) {\n    Ok(record) => record,\n    Err(e) if format!(\"{e:#}\").contains(\"failed to parse a js file as a module\") => {\n        // diagnostics were already emitted; in reduction runs this means 'candidate invalid'\n        return Ok(Default::default()); // or mark candidate as not interesting\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Feed only plain ESM JavaScript to dbg-swc commands built on parse_js","Strip TypeScript/Flow first with the matching SWC pass","During creduce sessions, treat parse failures on mutated files as the expected filter outcome"],"tags":["dbg-swc","parser","esm","syntax"],"backgroundTag":"javascript-syntax-error","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}