swc-project/swc · error

failed to run terser

Error message

failed to run terser

What it means

dbg-swc's terser comparison spawns 'npx terser <file> [--compress] [--mangle] --comments false' with stderr inherited and requires a zero exit; a non-zero terser exit aborts with this message. The underlying terser error is already visible on stderr above it. Typical causes: terser not resolvable by npx (offline, missing install) or terser itself failing to parse the input JS.

Source

Thrown at crates/dbg-swc/src/util/minifier.rs:93

    wrap_task(|| {
        let mut cmd = Command::new("npx");
        cmd.arg("terser");
        cmd.stderr(Stdio::inherit());

        if compress {
            cmd.arg("--compress");
        }
        if mangle {
            cmd.arg("--mangle");
        }
        cmd.args(["--comments", "false"]);
        cmd.arg("--");
        cmd.arg(file);

        let output = cmd.output().context("failed to get output")?;

        if !output.status.success() {
            bail!("failed to run terser");
        }

        let output = String::from_utf8(output.stdout).context("terser emitted non-utf8 string")?;

        // Drop comments
        let cm = Arc::new(SourceMap::default());
        let fm = cm.new_source_file(FileName::Anon.into(), output);
        let m = parse_js(fm)?;

        let code = print_js(cm, &m.module, true)?;

        Ok(code)
    })
    .with_context(|| format!("failed to get output of {} from terser", file.display()))
}

pub fn get_esbuild_output(file: &Path, mangle: bool) -> Result<String> {
    wrap_task(|| {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Run 'npx terser --version' in the same shell; install terser (npm i -g terser or as a devDependency) if it is missing
  2. Reproduce manually with the exact args: npx terser <file> --compress --mangle --comments false --
  3. Ensure registry access for npx or pre-install so npx resolves from node_modules
  4. If terser crashed on valid input, upgrade terser and re-run

Example fix

# before
$ dbg-swc es minifier ...   # terser not installed
error: failed to run terser

# after
$ npm install -g terser
$ npx terser --version
5.31.0
$ dbg-swc es minifier ...
Defensive patterns

Strategy: validation

Validate before calling

use std::process::Command;

fn terser_available() -> bool {
    Command::new("npx")
        .args(["terser", "--version"])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

// gate comparison runs on tool availability
assert!(terser_available(), "install terser: npm i -g terser");

Try / catch

match get_terser_output(&file, compress, mangle) {
    Ok(out) => out,
    Err(e) if format!("{e:#}").contains("failed to run terser") => {
        eprintln!("skipping terser diff: {e:#}");
        swc_output.clone() // degrade to swc-only comparison
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Running dbg-swc minifier comparison commands on a machine where 'npx terser' fails: package not installed and registry unreachable, npm auth/proxy issues, or a terser parse error on the candidate file (common during creduce mutation).

Common situations: Fresh CI containers without terser pre-installed; air-gapped environments where npx cannot fetch; mutated/invalid JS from test-case reduction that terser rejects before SWC comparison completes.

Related errors


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