{"record":{"id":"f5336e934183744b","repo":"swc-project/swc","slug":"watch-requires-out-file-or-out-dir","errorCode":null,"errorMessage":"--watch requires --out-file or --out-dir","messagePattern":"--watch requires --out-file or --out-dir","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_cli_impl/src/commands/compile.rs","lineNumber":509,"sourceCode":"\n        if let Some(source_maps) = &self.source_maps {\n            options.source_maps = Some(match source_maps.as_str() {\n                \"false\" => SourceMapsConfig::Bool(false),\n                \"true\" => SourceMapsConfig::Bool(true),\n                value => SourceMapsConfig::Str(value.to_string()),\n            });\n\n            self.source_file_name\n                .clone_into(&mut options.source_file_name);\n            self.source_root.clone_into(&mut options.source_root);\n        }\n\n        Ok(options)\n    }\n\n    fn validate(&self) -> anyhow::Result<()> {\n        if self.watch && self.out_file.is_none() && self.out_dir.is_none() {\n            bail!(\"--watch requires --out-file or --out-dir\");\n        }\n\n        if self.watch && self.files.is_empty() {\n            bail!(\"--watch requires input files\");\n        }\n\n        if self.copy_files && self.out_dir.is_none() {\n            bail!(\"--copy-files requires --out-dir\");\n        }\n\n        if self.strip_leading_paths && self.out_dir.is_none() {\n            bail!(\"--strip-leading-paths requires --out-dir\");\n        }\n\n        Ok(())\n    }\n\n    fn included_extensions(&self) -> Vec<String> {","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_cli_impl/src/commands/compile.rs#L491-L527","documentation":"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.","triggerScenarios":"Running `swc -w input.js` or `swc --watch ./src` without -o/--out-file/-d/--out-dir.","commonSituations":"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.","solutions":["Add an output destination: `swc -w ./src -d dist` or `swc -w input.js -o out.js`","Drop --watch if one-shot stdout output is what you want"],"exampleFix":"# before\nswc --watch ./src\n# after\nswc --watch ./src -d dist","handlingStrategy":"validation","validationCode":"# Shell - enforce watch preconditions\nif [[ \"$WATCH\" == \"1\" && -z \"$OUT_DIR$OUT_FILE\" ]]; then\n  echo \"--watch requires --out-file or --out-dir\" >&2; exit 1;\nfi\nswc $WATCH_FLAG $INPUTS $OUT_FLAGS","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair watch mode with an output destination in npm scripts","Template your swc commands so -w and -d are added together"],"tags":["swc-cli","watch","cli-options","validation"],"backgroundTag":"missing-required-cli-option","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}