denoland/deno · error · anyhow::Error

Could not resolve package req

Error message

Could not resolve package req '{}' from graph because it was missing in the provided npm snapshot.

What it means

When serializing npm modules into an eszip, Deno maps each npm package requirement found in the graph (module.pkg_req_ref) to a package id via the provided serialized npm resolution snapshot's root_packages table. If the requirement is absent from the snapshot, serialization aborts: the module graph references an npm package the snapshot never resolved. This indicates graph/snapshot skew, not a network problem.

Solutions

  1. Delete deno.lock (and DENO_DIR cache if reused) and let the build re-resolve graph and snapshot together
  2. Run `deno cache <entry>` successfully before compiling so graph and lockfile agree
  3. Ensure only one process/version of Deno produces both the graph and the npm snapshot in your build pipeline
  4. If it persists on a clean tree, report it — graph/snapshot skew on unmodified projects is a Deno bug

Example fix

# before
deno compile src/mod.ts  # Could not resolve package req 'foo@^2' ...

# after
rm deno.lock && deno cache src/mod.ts && deno compile src/mod.ts
Defensive patterns

Strategy: try-catch

Validate before calling

# Keep graph and npm snapshot in lockstep: regenerate both together
rm -f deno.lock && deno cache src/mod.ts  # exits non-zero if resolution fails

Try / catch

// In pipelines invoking deno compile
if (!cmd.success && stderr.includes("missing in the provided npm snapshot")) {
  // graph/snapshot skew: wipe lockfile + cache and rebuild once
  await run("rm -f deno.lock");
  await run("deno cache src/mod.ts");
  await run("deno compile -o app src/mod.ts");
}

Prevention

When it happens

Trigger: Building an eszip with a graph produced by one resolution pass and a snapshot (from deno.lock / ByonmNpmResolver) from another — e.g. lockfile updated between passes; embedding code reusing a cached graph after package.json changed; version mismatch between deno_graph semantics and the snapshot format.

Common situations: deno compile failing after editing package.json dependencies without regenerating deno.lock; CI caching DENO_DIR partially (graph cached, lock regenerated); custom tooling on the eszip crate feeding a stale SerializedNpmResolutionSnapshot.

Related errors


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

Appendix: source

Thrown at libs/eszip/v2.rs:1496

        }
        deno_graph::Module::Json(module) => {
          let eszip_module = EszipV2Module::Module {
            kind: ModuleKind::Json,
            source: EszipV2SourceSlot::Ready(module.source.text.clone().into()),
            source_map: EszipV2SourceSlot::Ready(Arc::new([])),
          };
          modules.insert(specifier_key.into_owned(), eszip_module);
          Ok(None)
        }
        deno_graph::Module::Npm(module) => {
          let Some(npm_packages) = npm_packages else {
            return Ok(None);
          };

          let req_ref = &module.pkg_req_ref;
          let serialize_npm_snapshot = npm_snapshot.as_serialized();
          let pkg_id = serialize_npm_snapshot.root_packages.get(req_ref.req())
            .ok_or_else(|| anyhow::anyhow!("Could not resolve package req '{}' from graph because it was missing in the provided npm snapshot.", req_ref.req()))?;
          let pkg_nv = &pkg_id.nv;
          let pkg_nv_reference =
            NpmPackageNvReference::new(PackageNvReference {
              nv: pkg_nv.clone(),
              sub_path: req_ref.sub_path().map(|s| s.into()),
            });

          if visited.should_visit_package_meta() {
            let meta_modules = npm_packages.take_meta_modules(pkg_nv.clone());
            let package_jsons = npm_packages.take_package_jsons(pkg_nv.clone());

            for meta_module in
              meta_modules.into_iter().chain(package_jsons).flatten()
            {
              modules.insert(
                meta_module.specifier,
                EszipV2Module::Module {
                  kind: ModuleKind::OpaqueData,

View on GitHub (pinned to 9ad36f7a2c)