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

Restore main field in package.json #342

Merged
merged 1 commit into from May 25, 2021

Conversation

mgcrea
Copy link
Contributor

@mgcrea mgcrea commented May 7, 2021

ky@0.28.0 can't be used in react-native apps anymore due to a missing main field in package.json.

Metro does provide configuration options to support extra "main" fields but adding "exports" there breaks as other packages start to use this field instead of their "main" (not sure why, as it is expected to be an ordered check, maybe a metro-related bug of the "exports" key handling when in object form). Haven't found a way to make it work except manually patching the ky's package.json.

error: Error: While trying to resolve module `ky` from file `~/foo/src/services/pumalocal/client.ts`, the package `~/foo/node_modules/ky/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`~/foo/node_modules/ky/index`. Indeed, none of these files exist:

@sholladay
Copy link
Collaborator

Issue #340 mentions other tools that are also breaking due to lack of main.

I'm personally in favor of merging this fix. Though, I'd really like to understand the root cause first. Ky is a valid package and Node's require() function can handle our usage of exports, so what are these tools doing that's causing problems?

@mgcrea
Copy link
Contributor Author

mgcrea commented May 7, 2021

I'm personally in favor of merging this fix. Though, I'd really like to understand the root cause first. Ky is a valid package and Node's require() function can handle our usage of exports, so what are these tools doing that's causing problems?

I think webpack or metro are designed to actively check specific keys in your package.json in a specific, user-configurable order, (eg. ['browser', 'module', 'main']) and bypass node require(). One use-case was to enable tree-shaking with the non-official "module" key before the "exports" field was spec'ed.

@sholladay
Copy link
Collaborator

Sure, I am aware of that behavior and it's a good point that the problem likely stems from how they detect which key to use. However, I would expect that once they find the exports key, they would pass its value to require(), which should work. Perhaps the implementation is more complicated than that, though.

@sholladay
Copy link
Collaborator

sholladay commented May 20, 2021

As I mentioned in #340 (comment), importing Ky from the unpkg CDN is also broken and I believe it's a similar root cause, meaning this will likely fix that, too. It would seem that a lot of the ecosystem is just implementing their own algorithms for finding the main entry point instead of using Node's require.resolve() or anything even remotely standardized.

@lpellegr
Copy link

I experienced the same issue using the last version of ky with a Typescript web app and ESM imports. This PR fixes the issue.

@mgcrea
Copy link
Contributor Author

mgcrea commented May 25, 2021

@sindresorhus any chance you could merge & release quickly this change as it is breaking many setups? Thanks!

@sindresorhus sindresorhus changed the title chore(npm): restore package.json main field to fix react-native bundl… Restore main field in package.json May 25, 2021
@sindresorhus sindresorhus merged commit 1338996 into sindresorhus:main May 25, 2021
@sindresorhus
Copy link
Owner

Sorry for the slow reply. Somehow I got accidentally unsubscribed to the Ky repo.

https://github.com/sindresorhus/ky/releases/tag/v0.28.2

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

Successfully merging this pull request may close these issues.

None yet

4 participants