denoland/deno · error

Failed to discover the newly created deno.json at "{}". This

Error message

Failed to discover the newly created deno.json at "{}". This can happen when the current directory is inside a node_modules directory.

What it means

When `deno add` needs a deno.json it doesn't have, it creates one and then re-discovers it via member_or_root_deno_json(). If discovery fails, the most common cause is the cwd being inside node_modules: config discovery deliberately skips node_modules trees, so the freshly written file is invisible.

Source

Thrown at cli/tools/pm/mod.rs:510

  };
  let deno_config = match start_dir.member_deno_json() {
    Some(deno_json) => Some(ConfigUpdater::new(
      ConfigKind::DenoJson,
      url_to_file_path(&deno_json.specifier)?,
    )?),
    None => None,
  };

  let (cli_factory, deno_config) = match deno_config {
    Some(config) => (cli_factory, Some(config)),
    None if npm_config.is_some() && !has_jsr_specifiers() => {
      (cli_factory, None)
    }
    _ => {
      let factory = create_deno_json(flags, options)?;
      let options = factory.cli_options()?.clone();
      let Some(deno_json) = options.start_dir.member_or_root_deno_json() else {
        bail!(
          "Failed to discover the newly created deno.json at \"{}\". This can happen when the current directory is inside a node_modules directory.",
          options.initial_cwd().join("deno.json").display(),
        );
      };
      (
        factory,
        Some(ConfigUpdater::new(
          ConfigKind::DenoJson,
          url_to_file_path(&deno_json.specifier)?,
        )?),
      )
    }
  };
  assert!(deno_config.is_some() || npm_config.is_some());
  Ok((cli_factory, force_package_json, npm_config, deno_config))
}

fn path_distance(a: &Path, b: &Path) -> usize {

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Move your work out of node_modules — treat it as disposable installed output.
  2. Run `deno add` from the parent project root so discovery finds the project's config.
  3. Pre-create deno.json yourself in the intended directory so deno never has to create-and-rediscover one.

Example fix

# before
cd node_modules/some-pkg && deno add npm:chalk
# after
cd ../..   # project root
deno add npm:chalk
Defensive patterns

Strategy: validation

Validate before calling

import { cwd } from "node:process";
if (cwd().split("/").includes("node_modules")) {
  console.error("refusing to run deno add inside node_modules; run from the project root");
  process.exit(1);
}

Prevention

When it happens

Trigger: Running `deno add <pkg>` inside a directory under node_modules (e.g. working on a vendored package), or any cwd where the just-created deno.json cannot be re-found by workspace discovery. Common trigger paths include scripts operating on node_modules contents, nested project scaffolds, patched-package workflows.

Common situations: Patching a dependency inside node_modules and running deno there; monorepos that nest projects under node_modules-adjacent layouts; CI jobs that cd into installed packages.

Related errors


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