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

[Experimental npm support] npm package implicit entry point not resolved correctly (cross-fetch) #15787

Closed
karelklima opened this issue Sep 6, 2022 · 2 comments
Labels
bug Something isn't working correctly node compat

Comments

@karelklima
Copy link

Popular cross-fetch polyfill NPM package (https://github.com/lquixada/cross-fetch) does not work with with the new npm: specifiers.

Example of code that results in an error

// Import from a package that has cross-fetch as a dependency
import { FetchDocumentLoader } from "npm:jsonld-context-parser";
const loader = new FetchDocumentLoader();

Error message

error: Uncaught Error: Cannot find module 'C:\...\deno\npm\registry.npmjs.org\cross-fetch\3.1.5\polyfill\dist\node-ponyfill.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (deno:ext/node/02_require.js:127:21)

Cause of the error

The cross-fetch polyfill is supposed to be imported as import 'cross-fetch/polyfill'. However, the package does not provide exports field in the root package.json. Instead there is a subfolder named polyfill containing a separate package.json file (https://github.com/lquixada/cross-fetch/blob/main/polyfill/package.json).

When resolving the module, Deno uses an entrypoint from the root package.json file, but also keeps the path to the subfolder in the module name, resulting in final path cross-fetch/3.1.5/polyfill/dist/node-ponyfill.js instead of cross-fetch/3.1.5/dist/node-ponyfill.js.

Possible solutions

  1. Let Deno resolve package.json files from subfolders in npm modules.
  2. Upstream fix - adding correct exports entry to cross-fetch fixes the issue, but it is probably not happening. There is an open PR from mid-2021: Support native node esm imports lquixada/cross-fetch#111

The cross-fetch package is quite a big deal as it has 9.6 million weekly downloads on NPM (react has 15.7 million).

Temporary solution

I have managed to make the package work by modifying its local copy in Deno cache by applying changes contained in the aforementioned PR: https://github.com/lquixada/cross-fetch/pull/111/files

Another possibility would be to remap the module using an import map to e.g. an empty module, but it is probably not yet supported with the new npm: specifiers (I wasn't able to make it work).

@bartlomieju bartlomieju added bug Something isn't working correctly node compat labels Sep 6, 2022
@bartlomieju
Copy link
Member

bartlomieju commented Dec 22, 2022

@karelklima I just tried the reproduction you provided and I don't get any error thrown (Deno v1.29.1). I believe this has been already fixed. I'm gonna close the issue for now, let me know if you encounter any more problems.

@karelklima
Copy link
Author

@bartlomieju Thank you for fixing this! I will resume working on the project where I encountered the problem and will let you know if there are any other similar issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly node compat
Projects
None yet
Development

No branches or pull requests

2 participants