denoland/deno · error · anyhow::Error

Use 'deno run' to run a local file directly, 'deno x' is…

Error message

Use 'deno run' to run a local file directly, 'deno x' is intended for running commands from packages.

What it means

`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.

Solutions

  1. Run local files with deno run: `deno run --allow-net ./serve.ts`.
  2. 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>`.

Example fix

# before
$ deno x ./serve.ts
error: Use 'deno run' to run a local file directly, 'deno x' is intended for running commands from packages.

# after
$ deno run --allow-net ./serve.ts
Defensive patterns

Strategy: validation

Validate before calling

# Route local files to `deno run`, packages to `deno x`
cmd="$1"; shift
case "$cmd" in
  ./*|/*|~/*) exec deno run "$@" "$cmd" ;;
  *) if [ -f "$cmd" ]; then exec deno run "$@" "$cmd"; else exec deno x "$cmd" "$@"; fi ;;
esac

Type guard

const isFileLike = (c: string) =>
  c.startsWith(".") || c.startsWith("/") || c.startsWith("~") ||
  c.startsWith("\\\") || /\.[A-Za-z0-9]+$/.test(c);
const useRun = (c: string) => isFileLike(c) && Deno.statSync(c).isFile;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/f7757883a9b40ec4. Report an issue: GitHub.

Appendix: source

Thrown at cli/tools/x.rs:611

  }

  // When --package is specified, the command is the binary name and
  // the package flag specifies which package to install. Combine them
  // into a single specifier like "npm:package/binary" so the existing
  // resolution flow handles it correctly.
  let effective_command = if let Some(ref package) = command_flags.package {
    format!("{}/{}", package, command_flags.command)
  } else {
    command_flags.command.clone()
  };

  let is_file_like = effective_command.starts_with('.')
    || effective_command.starts_with('/')
    || effective_command.starts_with('~')
    || effective_command.starts_with('\\')
    || Path::new(&effective_command).extension().is_some();
  if is_file_like && Path::new(&effective_command).is_file() {
    return Err(anyhow::anyhow!(
      "Use 'deno run' to run a local file directly, 'deno x' is intended for running commands from packages."
    ));
  }

  let thing_to_run = match deno_core::url::Url::parse(&effective_command) {
    Ok(url) => {
      if url.scheme() == "npm" {
        let req_ref = NpmPackageReqReference::from_specifier(&url)?;
        ReqRefOrUrl::Npm(req_ref)
      } else if url.scheme() == "jsr" {
        let req_ref = JsrPackageReqReference::from_specifier(&url)?;
        ReqRefOrUrl::Jsr(req_ref)
      } else {
        ReqRefOrUrl::Url(url)
      }
    }
    Err(deno_core::url::ParseError::RelativeUrlWithoutBase) => {
      let new_command = format!("npm:{}", effective_command);

View on GitHub (pinned to 9ad36f7a2c)