{"record":{"id":"47d31a83892df715","repo":"swc-project/swc","slug":"the-const-module-namespace-cannot-be-used-wit","errorCode":null,"errorMessage":"The const_module namespace `{}` cannot be used without member accessor","messagePattern":"The const_module namespace `(.+?)` cannot be used without member accessor","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_optimization/src/const_modules.rs","lineNumber":229,"sourceCode":"                n.visit_mut_children_with(self);\n            }\n        };\n    }\n\n    fn visit_mut_prop(&mut self, n: &mut Prop) {\n        match n {\n            Prop::Shorthand(id) => {\n                let sym_wtf8: Wtf8Atom = id.sym.clone().into();\n                if let Some(value) = self.scope.imported.get(&sym_wtf8) {\n                    *n = Prop::KeyValue(KeyValueProp {\n                        key: id.take().into(),\n                        value: Box::new((**value).clone()),\n                    });\n                    return;\n                }\n\n                if self.scope.namespace.contains(&id.to_id()) {\n                    panic!(\n                        \"The const_module namespace `{}` cannot be used without member accessor\",\n                        id.sym\n                    )\n                }\n            }\n            _ => n.visit_mut_children_with(self),\n        }\n    }\n}\n","sourceCodeStart":211,"sourceCodeEnd":239,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_optimization/src/const_modules.rs#L211-L239","documentation":"The const_modules pass (swc_ecma_transforms_optimization) inlines imports of modules you register as compile-time constant maps. Named/default imports are replaced by their literal values, but a namespace import (`import * as ns from '...'`) is only supported when accessed through a member (`ns.FLAG`), because the namespace itself has no single value. When such a binding is used bare as an object shorthand property (`{ ns }` in visit_mut_prop, const_modules.rs:216-233), the pass panics rather than emit incorrect code; the sibling arm at const_modules.rs:170 panics the same way for bare `ns` expressions.","triggerScenarios":"Running const_modules(globals) (e.g. via swc's jsc.experimental.constModules config) on a file with `import * as ns from '<module-listed-in-globals>'` followed by an object shorthand `{ ns }`, or any bare value use of `ns` (e.g. `console.log(ns)`).","commonSituations":"Refactoring named imports to namespace imports while using constant modules for feature flags/env values; copy-pasting code that spreads or logs the whole namespace (`{ ...ns }`, `{ ns }`).","solutions":["Switch to named imports from the const module (`import { FLAG } from '@app/constants'`) so each use inlines a literal value.","Change every bare use of the namespace to member access (`ns.FLAG`) before running the pass.","If you need the namespace as a real value, remove that module from the const-modules globals map so it stays a runtime import."],"exampleFix":"// before\nimport * as env from '@app/constants';\nconst cfg = { env };\n\n// after\nimport { env } from '@app/constants';\nconst cfg = { env };","handlingStrategy":"validation","validationCode":"// Before applying const_modules, ensure every namespace import from a\n// configured const module is only used via member access.\n#[derive(Default)]\nstruct FindBareNamespaceUse {\n    ns_ids: HashSet<Id>,\n    bad: Vec<Id>,\n}\nimpl Visit for FindBareNamespaceUse {\n    fn visit_member_expr(&mut self, m: &MemberExpr) {\n        // ns.member is fine; only inspect the property side\n        m.prop.visit_with(self);\n    }\n    fn visit_expr(&mut self, e: &Expr) {\n        if let Expr::Ident(i) = e {\n            if self.ns_ids.contains(&i.to_id()) {\n                self.bad.push(i.to_id());\n            }\n        }\n        e.visit_children_with(self);\n    }\n    fn visit_prop(&mut self, p: &Prop) {\n        if let Prop::Shorthand(i) = p {\n            if self.ns_ids.contains(&i.to_id()) {\n                self.bad.push(i.to_id());\n            }\n        }\n        p.visit_children_with(self);\n    }\n}","typeGuard":null,"tryCatchPattern":"// Last resort around a pass that panics:\nlet result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {\n    program.apply(const_modules(globals));\n}));\nif result.is_err() {\n    // surface file name + the const-module namespace usage to the user\n    return Err(anyhow!(\"const_modules rejected this file; check namespace imports\"));\n}","preventionTips":["Prefer named imports (`import { FLAG } from '@constants'`) over namespace imports when using const modules.","Codemod-rule your repo: forbid `import * as ns` for any module listed in the const-modules globals map.","Keep the globals map in review checklists so new namespace usages are caught at PR time."],"tags":["swc","const-modules","compiler-transform","import","panic"],"backgroundTag":"ast-transform-unsupported-node","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}