{"record":{"id":"7aaae30c082a6fab","repo":"denoland/deno","slug":"failed-to-discover-the-newly-created-deno-json-at","errorCode":null,"errorMessage":"Failed to discover the newly created deno.json at \"{}\". This can happen when the current directory is inside a node_modules directory.","messagePattern":"Failed to discover the newly created deno\\.json at \"(.+?)\"\\. This can happen when the current directory is inside a node_modules directory\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/tools/pm/mod.rs","lineNumber":510,"sourceCode":"  };\n  let deno_config = match start_dir.member_deno_json() {\n    Some(deno_json) => Some(ConfigUpdater::new(\n      ConfigKind::DenoJson,\n      url_to_file_path(&deno_json.specifier)?,\n    )?),\n    None => None,\n  };\n\n  let (cli_factory, deno_config) = match deno_config {\n    Some(config) => (cli_factory, Some(config)),\n    None if npm_config.is_some() && !has_jsr_specifiers() => {\n      (cli_factory, None)\n    }\n    _ => {\n      let factory = create_deno_json(flags, options)?;\n      let options = factory.cli_options()?.clone();\n      let Some(deno_json) = options.start_dir.member_or_root_deno_json() else {\n        bail!(\n          \"Failed to discover the newly created deno.json at \\\"{}\\\". This can happen when the current directory is inside a node_modules directory.\",\n          options.initial_cwd().join(\"deno.json\").display(),\n        );\n      };\n      (\n        factory,\n        Some(ConfigUpdater::new(\n          ConfigKind::DenoJson,\n          url_to_file_path(&deno_json.specifier)?,\n        )?),\n      )\n    }\n  };\n  assert!(deno_config.is_some() || npm_config.is_some());\n  Ok((cli_factory, force_package_json, npm_config, deno_config))\n}\n\nfn path_distance(a: &Path, b: &Path) -> usize {","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/cli/tools/pm/mod.rs#L492-L528","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Move your work out of node_modules — treat it as disposable installed output.","Run `deno add` from the parent project root so discovery finds the project's config.","Pre-create deno.json yourself in the intended directory so deno never has to create-and-rediscover one."],"exampleFix":"# before\ncd node_modules/some-pkg && deno add npm:chalk\n# after\ncd ../..   # project root\ndeno add npm:chalk","handlingStrategy":"validation","validationCode":"import { cwd } from \"node:process\";\nif (cwd().split(\"/\").includes(\"node_modules\")) {\n  console.error(\"refusing to run deno add inside node_modules; run from the project root\");\n  process.exit(1);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never run deno package commands from inside node_modules.","Pre-create deno.json in the real project so deno never auto-creates one.","Treat node_modules as disposable; do development in your source tree."],"tags":["deno-json","node-modules","discovery","cwd"],"backgroundTag":"config-discovery-failed","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T04:17:50.494Z"}