swc-project/swc · error

Value of `global_defs.{k}` must be a string literal:

Error message

Value of `global_defs.{k}` must be a string literal: 

What it means

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: '.

Source

Thrown at crates/swc_ecma_minifier/src/option/terser.rs:318

                        )
                        .map(drop_span)
                        .unwrap_or_else(|err| {
                            panic!("failed to parse `global_defs.{k}` of minifier options: {err:?}")
                        })
                    };
                    let key = parse(if let Some(k) = k.strip_prefix('@') {
                        k.to_string()
                    } else {
                        k.to_string()
                    });

                    (
                        key,
                        if k.starts_with('@') {
                            parse(
                                v.as_str()
                                    .unwrap_or_else(|| {
                                        panic!(
                                            "Value of `global_defs.{k}` must be a string literal: "
                                        )
                                    })
                                    .into(),
                            )
                        } else {
                            value_to_expr(v)
                        },
                    )
                })
                .collect(),
            hoist_fns: self.hoist_funs,
            hoist_props: self.hoist_props.unwrap_or(self.defaults),
            hoist_vars: self.hoist_vars,
            ie8: self.ie8,
            if_return: self.if_return.unwrap_or(self.defaults),
            inline: self
                .inline

View on GitHub (pinned to 5176682b65)

Solutions

  1. Quote the value so it is a string containing the expression: "@answer": "42" or "@dev": "process.env.NODE_ENV === 'development'"
  2. Drop the '@' prefix if you actually want raw JSON literal semantics (numbers/booleans handled by value_to_expr)
  3. Validate at config load: every key starting with '@' must map to a JSON string

Example fix

// before
{ "compress": { "global_defs": { "@/answer/": 42 } } } // panic: must be a string literal

// after
{ "compress": { "global_defs": { "@/answer/": "42" } } }
Defensive patterns

Strategy: validation

Validate before calling

// JS: @-prefixed global_defs keys require string values
for (const [k, v] of Object.entries(globalDefs)) {
  if (k.startsWith('@') && typeof v !== 'string') {
    throw new Error(`global_defs["${k}"] must be a string containing an expression`);
  }
}

Prevention

When it happens

Trigger: global_defs entries like { "@answer": 42 } or { "@flag": true } where the key starts with '@' but the JSON value is not a string.

Common situations: 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.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/52d80ffdc53e1305. Report an issue: GitHub.