{"record":{"id":"c08769ae800cee7c","repo":"tauri-apps/tauri","slug":"beforedevcommand-failed-with-exit-code","errorCode":null,"errorMessage":"beforeDevCommand `{}` failed with exit code {}","messagePattern":"beforeDevCommand `(.+?)` failed with exit code (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tauri-cli/src/dev.rs","lineNumber":195,"sourceCode":"        let mut command = Command::new(\"sh\");\n        command\n          .arg(\"-c\")\n          .arg(&before_dev)\n          .current_dir(cwd)\n          .envs(env);\n        command\n      };\n\n      if wait {\n        let status = command.piped().map_err(|error| Error::CommandFailed {\n          command: format!(\n            \"`{before_dev}` with `{}`\",\n            if cfg!(windows) { \"cmd /S /C\" } else { \"sh -c\" }\n          ),\n          error,\n        })?;\n        if !status.success() {\n          crate::error::bail!(\n            \"beforeDevCommand `{}` failed with exit code {}\",\n            before_dev,\n            status.code().unwrap_or_default()\n          );\n        }\n      } else {\n        command.stdin(Stdio::piped());\n        command.stdout(os_pipe::dup_stdout().unwrap());\n        command.stderr(os_pipe::dup_stderr().unwrap());\n\n        let child = SharedChild::spawn(&mut command)\n          .unwrap_or_else(|_| panic!(\"failed to run `{before_dev}`\"));\n\n        let child = BEFORE_DEV.get_or_init(move || child);\n        std::thread::spawn(move || {\n          let status = child\n            .wait()\n            .expect(\"failed to wait on \\\"beforeDevCommand\\\"\");","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/tauri-apps/tauri/blob/52e4b6e71d8632a7e648f866c442e287ecddee34/crates/tauri-cli/src/dev.rs#L177-L213","documentation":"When `beforeDevCommand` is configured in object form with `\"wait\": true`, `tauri dev` runs the script through `sh -c` (or `cmd /S /C` on Windows) in the frontend dir and waits for it. A non-zero exit code aborts dev mode, because the frontend server the app depends on never came up correctly.","triggerScenarios":"`tauri dev` with `\"beforeDevCommand\": { \"script\": \"npm run dev\", \"wait\": true }` where the script fails: missing devDependencies, a syntax error, an already-in-use port, or a typo'd script name. Only the `wait: true` object form surfaces the exit code; the plain string form spawns without waiting.","commonSituations":"Fresh clones where `npm install` was not run before `tauri dev`; scripts that fail only on some machines (port conflicts, missing env vars); switching package managers without updating the script string.","solutions":["Run the exact script manually in the frontend dir (e.g. `npm run dev`) and fix the underlying failure it reports","If dependencies are missing, install them (`npm install`) first","Check the script string for typos and that it matches a script defined in package.json; verify `wait: true` is what you want (string-form beforeDevCommand does not wait and would not raise this)"],"exampleFix":"// before (tauri.conf.json)\n\"beforeDevCommand\": { \"script\": \"npm run dev\", \"wait\": true }  // npm run dev exits 1\n\n// after: fix package.json so `npm run dev` succeeds\n\"scripts\": { \"dev\": \"vite\" }","handlingStrategy":"validation","validationCode":"# prove the dev script works before wiring it into tauri.conf.json\ncd frontend && npm ci && npm run dev &\nDEV_PID=$!; sleep 5\nkill $DEV_PID 2>/dev/null   # if it exited early, npm reports non-zero here","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Test the script standalone in the frontend dir before putting it in `beforeDevCommand`","Install node_modules after every fresh clone, before `tauri dev`","Prefer the string form of `beforeDevCommand` unless you truly need exit-code gating from `wait: true`"],"tags":["tauri-cli","dev-server","before-dev-command","shell","config"],"backgroundTag":"command-exit-code-nonzero","analyzedSha":"52e4b6e71d8632a7e648f866c442e287ecddee34","analyzedAt":"2026-08-20T13:59:20.734Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}