{"record":{"id":"a2f3bedd3bf7ef06","repo":"swc-project/swc","slug":"cannot-use-import-meta-outside-a-module","errorCode":null,"errorMessage":"Cannot use import.meta outside a module","messagePattern":"Cannot use import\\.meta outside a module","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"bindings/binding_nodejs_support_wasm/src/nodejs.rs","lineNumber":1144,"sourceCode":"\nfn import_binding_access(binding: &ImportBinding) -> String {\n    format!(\n        \"{}{}\",\n        binding.namespace,\n        export_property_access(&binding.export_name)\n    )\n}\n\nfn export_property_access(export_name: &str) -> String {\n    if is_identifier_name(export_name) {\n        format!(\".{export_name}\")\n    } else {\n        format!(\"[{}]\", json_string(export_name))\n    }\n}\n\nfn import_meta_error_expression() -> String {\n    \"(() => { throw new SyntaxError(\\\"Cannot use import.meta outside a module\\\"); })()\".to_string()\n}\n\nfn import_assignment_error_expression() -> String {\n    \"(() => { throw new TypeError(\\\"Assignment to constant variable.\\\"); })()\".to_string()\n}\n\nfn is_identifier_name(value: &str) -> bool {\n    let mut chars = value.chars();\n    let Some(first) = chars.next() else {\n        return false;\n    };\n\n    if !(first == '_' || first == '$' || first.is_ascii_alphabetic()) {\n        return false;\n    }\n\n    chars.all(|ch| ch == '_' || ch == '$' || ch.is_ascii_alphanumeric())\n}","sourceCodeStart":1126,"sourceCodeEnd":1162,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/bindings/binding_nodejs_support_wasm/src/nodejs.rs#L1126-L1162","documentation":"The tree-construction step hit a token that cannot legally appear while the parser is in the Start phase (before the root element). Every token type has an explicit arm for the Start phase except the end-tag token, which falls into the `_` catch-all and produces this error; a CDATA section in the prolog is also reported through this same ErrorKind. It is recoverable: the token is simply dropped and parsing continues.","triggerScenarios":"Input containing a stray end tag before any start tag, e.g. `</root>` at the very beginning, or a stray `</item>` left over from fragment concatenation; or a CDATA section (`<![CDATA[...]]>`) appearing before the root element (that arm also calls append_cdata_to_doc after reporting). Both are reported as ErrorKind::UnexpectedTokenInStartPhase.","commonSituations":"Concatenating XML fragments (e.g. log chunks or split files) so that a closing tag from a previous chunk lands before the new root element; template engines emitting `]]>`-style raw blocks in the prolog; hand-edited XML where a closing tag was kept after deleting its opening tag.","solutions":["Look at the span in the error to find the exact offset of the offending token, then remove the stray end tag or CDATA section that sits before the root element.","If you are concatenating fragments, wrap them in a single container element instead of gluing raw chunks together.","Move any CDATA payload inside the root element where character data is allowed.","Run the input through a validating XML tool (xmllint or an IDE XML linter) to locate pre-root junk quickly."],"exampleFix":"<!-- before: stray end tag and CDATA before root -->\n</config>\n<![CDATA[raw]]>\n<config/>\n\n<!-- after -->\n<config>\n  <![CDATA[raw]]>\n</config>","handlingStrategy":"validation","validationCode":"use swc_xml_parser::error::ErrorKind;\n\nlet mut errors = Vec::new();\nlet _doc = swc_xml_parser::parse_file_as_document(&fm, config, &mut errors)?;\nlet stray = errors\n    .iter()\n    .filter(|e| matches!(e.kind(), ErrorKind::UnexpectedTokenInStartPhase))\n    .count();\nif stray > 0 {\n    eprintln!(\"{stray} stray token(s) before the root element\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never concatenate raw XML fragments; build one tree and serialize it once.","Keep the prolog restricted to the XML declaration, doctype, comments, and processing instructions.","Lint fixture inputs with a strict XML checker before tests run."],"tags":["xml","parser","stray-tag","prolog","swc","recoverable"],"backgroundTag":"malformed-xml-document","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}