deno icon indicating copy to clipboard operation
deno copied to clipboard

import.meta.resolve with npm packages

Open oscarotero opened this issue 7 months ago • 4 comments

Related: https://github.com/denoland/deno/issues/16089

Every time I try to resolve a npm package:

const preact_specifier = import.meta.resolve("npm:preact");

I get the error:

Uncaught TypeError: "npm:" specifiers are currently not supported in import.meta.resolve()

I would rather the old behavior, returning simply npm:preact. This error is very unfortunated because now it's not possible to get the final specifier if the specifier is mapped in the import map:

{
  "imports": {
    "npm:preact": "https://esm.sh/preact"
  }
}

I guess this is the intended behavior would like to know the explanation for that decision.

oscarotero avatar Nov 22 '23 16:11 oscarotero

Strictly speaking, the specifier being loaded in the module registry is npm:preact in this case (and so far as it only has one representation in a given Deno application lifetime). So that this does sound like the sensible behaviour to me, while still treating the real cache location etc as an internal implementation detail.

guybedford avatar Nov 23 '23 06:11 guybedford

To clarify, the sensible behavior to me is:

import.meta.resolve("npm:preact"); // Returns "npm:preact"

With this import map:

{
  "imports": {
    "npm:preact": "https://esm.sh/preact"
  }
}
import.meta.resolve("npm:preact"); // Returns "https://esm.sh/preact"

oscarotero avatar Nov 23 '23 11:11 oscarotero

I agree.

lucacasonato avatar Nov 23 '23 13:11 lucacasonato

Opened https://github.com/denoland/deno_core/pull/354 that will make it easier to customize.

bartlomieju avatar Nov 26 '23 22:11 bartlomieju