URL-Based vs. Relative Import Resolution in Deno
0 reputation · 09 Dec 2021, 23:17 UTC
0 reputation · 09 Dec 2021, 23:17 UTC
The goal is to ensure consistent module loading behavior across a project utilizing both remote and local dependencies.
The exact behavior when mixing URL-based imports with relative path imports within the same module set remains subject to version nuances.
Deno does not prioritize URL imports over relative imports. There is no precedence rule that lets a URL shadow or override a matching relative path. Each import specifier is resolved independently based on its own form:
./ or ../ specifiers are relative paths resolved against the directory of the importing module.https://, http://, or file:// specifiers are resolved as URLs.lodash fail unless an import map remaps them.Deno keys its module cache by the fully resolved URL. Consequently, ./mod.ts and file:///absolute/path/to/mod.ts can point to the same file on disk yet be treated as separate modules, creating duplicate module instances when both are imported.
This URL-based core is stable across Deno 1.x and 2.x for standard HTTP/HTTPS and relative specifiers. Deno 2 changed npm: specifier handling and some type-only import behavior, but not the relative-versus-URL decision.
When a project mixes remote and local dependencies, Deno resolves each import line in isolation. A relative path is never matched against an existing URL import to decide where to load from. If the relative path does not resolve to a file relative to the importer, resolution fails even if a same-named URL import exists elsewhere in the graph.
deno --version. This guidance assumes standard HTTP/HTTPS and relative specifiers; if you use npm: specifiers, share the version because Deno 2 handles them differently.main.ts importing both forms:import { localValue } from "./local.ts";
import { remoteValue } from "https://example.com/remote.ts";
console.log(localValue, remoteValue);
deno info main.ts
Confirm that the relative import appears under its local path and the remote URL appears as a separate entry. If both refer to equivalent logic, check whether they are listed once or twice to detect duplicate caching.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 10 Dec 2021, 09:45 UTC
Deno does not give URL imports precedence over relative imports; each specifier is resolved independently. A relative path such as ./mod.ts becomes a file:// URL relative to the importing file, while https://example.com/mod.ts stays a remote specifier. Because the module cache is keyed by the fully resolved URL, the two forms do not alias each other and may produce duplicate module state if both are loaded. The URL-centric model is stable across Deno 1.x and 2.x, but verify with deno --version and inspect the graph using deno info entry.ts, since import map and package.json handling changed between major releases.