{"record":{"id":"f7757883a9b40ec4","repo":"denoland/deno","slug":"use-deno-run-to-run-a-local-file-directly-deno","errorCode":null,"errorMessage":"Use 'deno run' to run a local file directly, 'deno x' is intended for running commands from packages.","messagePattern":"Use 'deno run' to run a local file directly, 'deno x' is intended for running commands from packages\\.","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"cli/tools/x.rs","lineNumber":611,"sourceCode":"  }\n\n  // When --package is specified, the command is the binary name and\n  // the package flag specifies which package to install. Combine them\n  // into a single specifier like \"npm:package/binary\" so the existing\n  // resolution flow handles it correctly.\n  let effective_command = if let Some(ref package) = command_flags.package {\n    format!(\"{}/{}\", package, command_flags.command)\n  } else {\n    command_flags.command.clone()\n  };\n\n  let is_file_like = effective_command.starts_with('.')\n    || effective_command.starts_with('/')\n    || effective_command.starts_with('~')\n    || effective_command.starts_with('\\\\')\n    || Path::new(&effective_command).extension().is_some();\n  if is_file_like && Path::new(&effective_command).is_file() {\n    return Err(anyhow::anyhow!(\n      \"Use 'deno run' to run a local file directly, 'deno x' is intended for running commands from packages.\"\n    ));\n  }\n\n  let thing_to_run = match deno_core::url::Url::parse(&effective_command) {\n    Ok(url) => {\n      if url.scheme() == \"npm\" {\n        let req_ref = NpmPackageReqReference::from_specifier(&url)?;\n        ReqRefOrUrl::Npm(req_ref)\n      } else if url.scheme() == \"jsr\" {\n        let req_ref = JsrPackageReqReference::from_specifier(&url)?;\n        ReqRefOrUrl::Jsr(req_ref)\n      } else {\n        ReqRefOrUrl::Url(url)\n      }\n    }\n    Err(deno_core::url::ParseError::RelativeUrlWithoutBase) => {\n      let new_command = format!(\"npm:{}\", effective_command);","sourceCodeStart":593,"sourceCodeEnd":629,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/cli/tools/x.rs#L593-L629","documentation":"`deno x` exists to run commands from packages, not local files. If the command token looks file-like (starts with `.`, `/`, `~`, `\\`, or has a file extension) AND that path exists on disk, x.rs:603-613 refuses and tells you to use `deno run` — running a local file through the x auto-install machinery would silently do the wrong thing.","triggerScenarios":"deno x ./script.ts, deno x main.ts (when main.ts exists in cwd), deno x ~/tools/foo.js; also a package name that collides with an existing local file, e.g. deno x foo.bar when ./foo.bar exists.","commonSituations":"Muscle memory from `npx .`-style workflows; a new file lands in cwd whose name matches the intended package; tutorials switching between deno run and deno x.","solutions":["Run local files with deno run: `deno run --allow-net ./serve.ts`.","If the token really is a package whose name looks like a file, remove/rename the colliding local file or use the explicit form `deno x npm:<pkg>` / `deno x jsr:<pkg>`."],"exampleFix":"# before\n$ deno x ./serve.ts\nerror: Use 'deno run' to run a local file directly, 'deno x' is intended for running commands from packages.\n\n# after\n$ deno run --allow-net ./serve.ts","handlingStrategy":"validation","validationCode":"# Route local files to `deno run`, packages to `deno x`\ncmd=\"$1\"; shift\ncase \"$cmd\" in\n  ./*|/*|~/*) exec deno run \"$@\" \"$cmd\" ;;\n  *) if [ -f \"$cmd\" ]; then exec deno run \"$@\" \"$cmd\"; else exec deno x \"$cmd\" \"$@\"; fi ;;\nesac","typeGuard":"const isFileLike = (c: string) =>\n  c.startsWith(\".\") || c.startsWith(\"/\") || c.startsWith(\"~\") ||\n  c.startsWith(\"\\\\\\\") || /\\.[A-Za-z0-9]+$/.test(c);\nconst useRun = (c: string) => isFileLike(c) && Deno.statSync(c).isFile;","tryCatchPattern":null,"preventionTips":["Reach for `deno run` for anything local; `deno x` only for package commands.","Avoid package names that collide with existing local filenames, or use the explicit npm:/jsr: form.","Remember the heuristic: leading ./, /, ~, \\\\ or a file extension plus an existing file means run, not x."],"tags":["deno-x","run","cli"],"backgroundTag":"wrong-subcommand-invocation","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}