{"record":{"id":"52d80ffdc53e1305","repo":"swc-project/swc","slug":"value-of-global-defs-k-must-be-a-string-litera","errorCode":null,"errorMessage":"Value of `global_defs.{k}` must be a string literal: ","messagePattern":"Value of `global_defs\\.(.+?)` must be a string literal: ","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_minifier/src/option/terser.rs","lineNumber":318,"sourceCode":"                        )\n                        .map(drop_span)\n                        .unwrap_or_else(|err| {\n                            panic!(\"failed to parse `global_defs.{k}` of minifier options: {err:?}\")\n                        })\n                    };\n                    let key = parse(if let Some(k) = k.strip_prefix('@') {\n                        k.to_string()\n                    } else {\n                        k.to_string()\n                    });\n\n                    (\n                        key,\n                        if k.starts_with('@') {\n                            parse(\n                                v.as_str()\n                                    .unwrap_or_else(|| {\n                                        panic!(\n                                            \"Value of `global_defs.{k}` must be a string literal: \"\n                                        )\n                                    })\n                                    .into(),\n                            )\n                        } else {\n                            value_to_expr(v)\n                        },\n                    )\n                })\n                .collect(),\n            hoist_fns: self.hoist_funs,\n            hoist_props: self.hoist_props.unwrap_or(self.defaults),\n            hoist_vars: self.hoist_vars,\n            ie8: self.ie8,\n            if_return: self.if_return.unwrap_or(self.defaults),\n            inline: self\n                .inline","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_minifier/src/option/terser.rs#L300-L336","documentation":"For @-prefixed global_defs keys (e.g. \"@/regex/\" or \"@answer\"), the value string itself is parsed as a JavaScript expression, so the JSON value must be a string. A non-string value (number, boolean, object, array) fails v.as_str() and panics with 'Value of `global_defs.{k}` must be a string literal: '.","triggerScenarios":"global_defs entries like { \"@answer\": 42 } or { \"@flag\": true } where the key starts with '@' but the JSON value is not a string.","commonSituations":"Porting terser configs where numeric defines work fine for non-@ keys (value_to_expr handles numbers/bools there), assuming @-defines accept raw JSON values, config generators that drop quotes.","solutions":["Quote the value so it is a string containing the expression: \"@answer\": \"42\" or \"@dev\": \"process.env.NODE_ENV === 'development'\"","Drop the '@' prefix if you actually want raw JSON literal semantics (numbers/booleans handled by value_to_expr)","Validate at config load: every key starting with '@' must map to a JSON string"],"exampleFix":"// before\n{ \"compress\": { \"global_defs\": { \"@/answer/\": 42 } } } // panic: must be a string literal\n\n// after\n{ \"compress\": { \"global_defs\": { \"@/answer/\": \"42\" } } }","handlingStrategy":"validation","validationCode":"// JS: @-prefixed global_defs keys require string values\nfor (const [k, v] of Object.entries(globalDefs)) {\n  if (k.startsWith('@') && typeof v !== 'string') {\n    throw new Error(`global_defs[\"${k}\"] must be a string containing an expression`);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember terser semantics: '@key' means value is parsed as an expression string","Quote all @-define values, including numbers","Use a JSON schema (additionalProperties with string values) for @-prefixed entries"],"tags":["minifier","terser","global-defs","type-mismatch","config"],"backgroundTag":"config-type-mismatch","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"}