swc-project/swc · error

--watch requires --out-file or --out-dir

Error message

--watch requires --out-file or --out-dir

What it means

swc CLI validation: watch mode recompiles files and writes results on change, which requires knowing where to write. With neither --out-file nor --out-dir given, validate() rejects --watch before any compilation starts.

Source

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

        if let Some(source_maps) = &self.source_maps {
            options.source_maps = Some(match source_maps.as_str() {
                "false" => SourceMapsConfig::Bool(false),
                "true" => SourceMapsConfig::Bool(true),
                value => SourceMapsConfig::Str(value.to_string()),
            });

            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> {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add an output destination: `swc -w ./src -d dist` or `swc -w input.js -o out.js`
  2. Drop --watch if one-shot stdout output is what you want

Example fix

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

Strategy: validation

Validate before calling

# Shell - enforce watch preconditions
if [[ "$WATCH" == "1" && -z "$OUT_DIR$OUT_FILE" ]]; then
  echo "--watch requires --out-file or --out-dir" >&2; exit 1;
fi
swc $WATCH_FLAG $INPUTS $OUT_FLAGS

Prevention

When it happens

Trigger: Running `swc -w input.js` or `swc --watch ./src` without -o/--out-file/-d/--out-dir.

Common situations: Assuming watch prints to stdout like single-file mode; porting commands from other bundlers (tsc --watch works without outDir); incomplete flag aliases in npm scripts.

Related errors


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