[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import.meta.resolve doesn't work with npm specifiers. #16089

Closed
oscarotero opened this issue Sep 29, 2022 · 9 comments
Closed

import.meta.resolve doesn't work with npm specifiers. #16089

oscarotero opened this issue Sep 29, 2022 · 9 comments

Comments

@oscarotero
Copy link

The following code returns the same npm specifier instead of the url of the resolved file:

const reactLocation = import.meta.resolve("npm:react");

// npm:react
@dsherret dsherret added bug Something isn't working correctly node compat labels Sep 29, 2022
@bartlomieju
Copy link
Member

I believe this is correct. If you do console.log(import.meta.resolve("https://deno.land/std@0.158.0/http/file_server.ts")); you will get "https://deno.land/std@0.158.0/http/file_server.ts", not the path to Deno cache.

@oscarotero
Copy link
Author

@bartlomieju Is there any way to get the path of the cached file? I'm using it with esbuild to resolve and load dependencies for bundling.

@bartlomieju
Copy link
Member

Not currently, no. You can do that, if you know the exact version you want to use though - deno info gives you information where a cache for npm packages resides and with that you can construct the path yourself.

It is something I also encountered when trying to write a plugin for rollup; so we will probably look into some more ergonomic way.

@deckchairlabs
Copy link

@oscarotero @bartlomieju Adding an import before the import.meta.resolve returns the path of the cached file.

import React from "npm:react";

console.log(import.meta.resolve("npm:react"));

@oscarotero
Copy link
Author

This is a inconsistency and it doesn't work in all cases. This code returns an error

const specifier = "npm:react";
await import(specifier);

console.log(import.meta.resolve(specifier));
/* error: Uncaught (in promise) TypeError: Loading unprepared module: npm:react, imported from: file:///Users/oscarotero/www/test/deno/main.js */

For now, I'm using esm.sh to get the content of the npm packages: https://github.com/lumeland/lume/blob/6cca5823609ee3c036e7e51d918a304067bc9066/plugins/esbuild.ts#L82-L85

@dsherret dsherret mentioned this issue Oct 5, 2022
25 tasks
@guybedford
Copy link
Contributor

Thinking in terms of a request URL / response URL may offer a little more flexibility here in in trying to reconcile any differences between import.meta.url and import.meta.resolve. Redirects allow the response URL (as import.meta.url represents) to be different to the request URL. import.meta.resolve then corresponds to the request URL as it is the client side resolver, before redirects are applied.

@oscarotero
Copy link
Author

There's a casuistry that must be taken into account: the npm package that is not cached (because it wasn't imported previously). If it's decided that import.meta.resolve("npm:react") must return the cached file, but it isn't already cached, this means Deno must download and cache the package before return the filename.

@guybedford
Copy link
Contributor

@oscarotero import.meta.resolve is synchronous uses the current resolver cache. This can be distinct from what the tracing loader is doing asynchronously. It is possible for import.meta.resolve to fail where dynamic import() would pass due to this distinction.

@bartlomieju
Copy link
Member

Closed in #21716.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants