swc-project/swc · error

--copy-files requires --out-dir

Error message

--copy-files requires --out-dir

What it means

swc CLI validation: --copy-files copies non-compilable assets (JSON, images, etc.) from the input tree into the output tree, which is only meaningful with a directory-shaped output. Without --out-dir, validate() rejects the flag.

Source

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

            self.source_file_name
                .clone_into(&mut options.source_file_name);
            self.source_root.clone_into(&mut options.source_root);
        }

        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()
        })
    }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add --out-dir: `swc ./src -d dist --copy-files`
  2. Or drop --copy-files when compiling a single file to stdout

Example fix

# before
swc --copy-files ./src
# after
swc ./src -d dist --copy-files
Defensive patterns

Strategy: validation

Validate before calling

const needsOutDir = Boolean(argv['copy-files'] || argv['strip-leading-paths']);
if (needsOutDir && !argv['out-dir']) {
  throw new Error('--copy-files requires --out-dir');
}

Prevention

When it happens

Trigger: Running `swc --copy-files input.js` (single-file mode, no -d), where there is no output directory to copy assets into.

Common situations: Copying a command from a directory-mode example but running it on a single file; removing -d while iterating and forgetting the coupled flag.

Related errors


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