{"record":{"id":"06799e344dfe634a","repo":"swc-project/swc","slug":"the-const-module-namespace-sym-cannot-be-used","errorCode":null,"errorMessage":"The const_module namespace `{sym}` 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":171,"sourceCode":"\n        if self.scope.imported.is_empty() && self.scope.namespace.is_empty() {\n            return;\n        }\n\n        n.visit_mut_children_with(self);\n    }\n\n    fn visit_mut_expr(&mut self, n: &mut Expr) {\n        match n {\n            Expr::Ident(ref id @ Ident { ref sym, .. }) => {\n                let sym_wtf8: Wtf8Atom = sym.clone().into();\n                if let Some(value) = self.scope.imported.get(&sym_wtf8) {\n                    *n = (**value).clone();\n                    return;\n                }\n\n                if self.scope.namespace.contains(&id.to_id()) {\n                    panic!(\n                        \"The const_module namespace `{sym}` cannot be used without member accessor\"\n                    )\n                }\n            }\n            Expr::Member(MemberExpr { obj, prop, .. }) if obj.is_ident() => {\n                if let Some(module_name) = obj\n                    .as_ident()\n                    .filter(|member_obj| self.scope.namespace.contains(&member_obj.to_id()))\n                    .map(|member_obj| &member_obj.sym)\n                {\n                    let imported_name: Wtf8Atom = match prop {\n                        MemberProp::Ident(ref id) => id.sym.clone().into(),\n                        MemberProp::Computed(ref p) => match &*p.expr {\n                            Expr::Lit(Lit::Str(s)) => s.value.clone(),\n                            _ => return,\n                        },\n                        MemberProp::PrivateName(..) => return,\n                        #[cfg(swc_ast_unknown)]","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_optimization/src/const_modules.rs#L153-L189","documentation":"`import * as NS from 'mod'` registers NS as a namespace binding for a const module. The transform can only substitute concrete member accesses (NS.member); using NS itself as an expression value (not as the object of a member access) panics with 'The const_module namespace `{sym}` cannot be used without member accessor' because there is no namespace object to materialize.","triggerScenarios":"Code like `import * as env from 'env'; console.log(env);` or passing/broadcasting the namespace identifier anywhere except `NS.member` position; MemberProp::PrivateName accesses also bypass substitution.","commonSituations":"Debug logging of a namespace, spreading a namespace, generic wrappers receiving the namespace object, refactors that keep a namespace import while new code references the bare identifier.","solutions":["Reference concrete members: env.MODE instead of env","Convert the namespace import to named imports for the members actually used","Remove the namespace import if nothing accesses members through it"],"exampleFix":"// before\nimport * as env from '@app/env';\nconsole.log(env); // bare namespace as value -> panic\n\n// after\nimport * as env from '@app/env';\nconsole.log(env.MODE);","handlingStrategy":"validation","validationCode":"// JS: flag bare namespace identifiers used as values\nfunction checkNamespaceUsage(src, globals) {\n  const mods = new Set(Object.keys(globals));\n  const re = /import\\s*\\*\\s*as\\s+([\\w$]+)\\s+from\\s+['\"]([^'\"]+)['\"]/g;\n  for (const [, local, mod] of src.matchAll(re)) {\n    if (!mods.has(mod)) continue;\n    const use = new RegExp(`(?<![.\\w$])${local}(?![.\\w$])`);\n    if (use.test(src.split(/import[^;]+;/).slice(1).join('')))\n      throw new Error(`namespace \"${local}\" used without member accessor`);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always access const-module namespaces through members (NS.MEMBER)","Convert namespace imports to named imports when only a few members are used","Lint against passing namespace identifiers as values in codebases using const modules"],"tags":["const-modules","namespace","import","member-access"],"backgroundTag":"namespace-import-misuse","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"}