swc-project/swc · error

--strip-leading-paths requires --out-dir

Error message

--strip-leading-paths requires --out-dir

What it means

swc CLI validation: --strip-leading-paths removes leading path segments (like ./src) from output file paths, which only makes sense when writing a directory tree. Without --out-dir the flag has no effect target, so validate() rejects the combination.

Source

Thrown at crates/swc_cli_impl/src/commands/compile.rs:521

        Ok(options)
    }

    fn validate(&self) -> anyhow::Result<()> {
        if self.watch && self.out_file.is_none() && self.out_dir.is_none() {
            bail!("--watch requires --out-file or --out-dir");
        }

        if self.watch && self.files.is_empty() {
            bail!("--watch requires input files");
        }

        if self.copy_files && self.out_dir.is_none() {
            bail!("--copy-files requires --out-dir");
        }

        if self.strip_leading_paths && self.out_dir.is_none() {
            bail!("--strip-leading-paths requires --out-dir");
        }

        Ok(())
    }

    fn included_extensions(&self) -> Vec<String> {
        self.extensions.clone().unwrap_or_else(|| {
            DEFAULT_EXTENSIONS
                .iter()
                .map(|value| value.to_string())
                .collect()
        })
    }

    fn has_directory_input(&self) -> bool {
        self.files.iter().any(|path| path.is_dir())
    }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add --out-dir: `swc ./src -d dist --strip-leading-paths`
  2. Or remove the flag if compiling without an output directory

Example fix

# before
swc --strip-leading-paths ./src
# after
swc ./src -d dist --strip-leading-paths
Defensive patterns

Strategy: validation

Validate before calling

if (argv['strip-leading-paths'] && !argv['out-dir']) {
  throw new Error('--strip-leading-paths requires --out-dir');
}

Prevention

When it happens

Trigger: Running `swc --strip-leading-paths input.js` or any invocation with the flag but no -d/--out-dir.

Common situations: Flags carried over from directory-mode commands into single-file mode; deleting the -d flag during debugging while leaving dependent flags.

Related errors


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