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

React Native: Package subpath './package.json' is not defined by "exports" #1054

Closed
brianlenz opened this issue Jun 16, 2022 · 6 comments
Closed

Comments

@brianlenz
Copy link

When using domutils v3 in a React Native project, you'll get a warning when building:

warn Package domutils has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in $PATH/node_modules/domutils/package.json

This is something that has been reported for multiple packages in React Native here:

react-native-community/cli#1168

Each individual package maintainer has the ability to work around the issue, however, by simply exporting package.json. I'll submit a PR to do this and leave it up to you @fb55 if you'd like to merge.

@fb55
Copy link
Owner

fb55 commented Jun 18, 2022

Hi @brianlenz, do you know why the package.json is loaded in the first place?

@brianlenz
Copy link
Author

Hi @fb55, honestly, I don't fully understand the background, but from the issue I linked, here's the explanation:

The reason is that, starting in Node.js 14.x, as soon as an npm module defines the exports field in package.json, only the files listed there are exported. If package.json is not included in that list, it's no longer possible to do things like require.resolve('uuid/package.json');.

The problematic piece of code is:

https://github.com/react-native-community/cli/blob/5819a17d148726a1185420b6df345dad6c81c81b/packages/cli/src/tools/config/resolveNodeModuleDir.ts#L11-L13

And then there is further background provided in a comment here:

react-native-community/cli#1168 (comment)

Given that the issue has been open in React Native for years, it seems there isn't a resolution coming on their end anytime soon. Most other projects in this situation have addressed the problem as I addressed it in the PR here (which you can see by all of the links to other projects in the issue).

Since React Native is such a popular framework, I figured it would be best for domutils to apply the same workaround to prevent the warning.

@fb55
Copy link
Owner

fb55 commented Jul 25, 2022

I will follow Sindre's lead here (react-native-community/cli#1168 (comment)), and won't be adding the package.json file to itself. As a workaround, you can use an earlier version of this module that doesn't provide an export map yet (the 2.x releases are fine). Hope that's okay!

@fb55 fb55 closed this as completed Jul 25, 2022
@brianlenz
Copy link
Author

@fb55, bummer, that's too bad. I guess we are stuck patching your package for the foreseeable future 😩 For anyone else who comes here with the same issue, you can use this patch file to fix it (either with Yarn 2 patch or patch-package):

diff --git a/package.json b/package.json
index 7955573aded67b578c6014d5e1bc94056f67f5a1..edc4e30427ff7e01b7869f3ea8b81b74488b1dcb 100644
--- a/package.json
+++ b/package.json
@@ -12,8 +12,11 @@
     "types": "lib/index.d.ts",
     "module": "lib/esm/index.js",
     "exports": {
-        "require": "./lib/index.js",
-        "import": "./lib/esm/index.js"
+        "./package.json": "./package.json",
+        ".": {
+            "require": "./lib/index.js",
+            "import": "./lib/esm/index.js"
+        }
     },
     "files": [
         "lib/**/*"

@fb55
Copy link
Owner

fb55 commented Jul 25, 2022

Is there a particular feature you need from the 3.x releases? Staying on an older version seems like much simpler solution here — but don't let me stop you from doing what's best for you.

@brianlenz
Copy link
Author

Is there a particular feature you need from the 3.x releases? Staying on an older version seems like much simpler solution here — but don't let me stop you from doing what's best for you.

@fb55, I appreciate the consideration and help! 🙏 In our case, it's a transitive dependency that we don't have direct control over. I suppose forcing a downgrade through resolution may work, but it doesn't seem like a particularly good idea.

It may not ultimately matter, as it looks like react-native-cli may actually be addressing the root cause, though! Just now:

react-native-community/cli#1655

Glad to see after 2+ years 😁

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 a pull request may close this issue.

2 participants