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

Handle non-exported package.json files #108

Closed
wants to merge 1 commit into from

Conversation

jpaquim
Copy link

@jpaquim jpaquim commented May 18, 2020

node's resolve throws an ERR_PACKAGE_PATH_NOT_EXPORTED error when trying
to resolve the package.json of a package which does not list itself in
its "exports" object.

This works around the issue by handling this case separately, using the
error message to get the path to the file, which is then required
correctly by the existing call to tryRequire().

In my case, this was happening with the uuid package, which has an "exports", but does not list the package.json itself in it, so node then complains.

I now see that the issue is already under discussion at #104, and in the uuid repo itself: uuidjs/uuid#444

Please let me know what you think of the proposed solution, I'm not sure if it's too workaround-ish...

node's resolve throws an ERR_PACKAGE_PATH_NOT_EXPORTED error when trying
to resolve the package.json of a package which does not list itself in
its "exports" object.

This works around the issue by handling this case separately, using the
error message to get the path to the file, which is then required
correctly by the existing call to tryRequire().
// in the edge cases where the package.json file is not listed in the
// package's "exports" field, parse the target file name from the error
// message
return tryParseErrorMessage(err);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that parsing the error message is a good idea. I think the error message is nothing one could rely on, it could change at any point in time.

Instead, I believe that right now the plugin should probably just emit a warning if it cannot resolve the package.json so that package authors can start exporting it if necessary (it's not always necessary, in the case of uuid it is definitely not necessary for the svelte plugin to read uuid's package.json since it doesn't contain any svelte-specific information).

See: uuidjs/uuid#444 (comment)

We might even see Node.js change it's behavior again and always make package.json resolvable, see nodejs/node#33460

ctavan added a commit to ctavan/rollup-plugin-svelte that referenced this pull request May 18, 2020
@ctavan
Copy link

ctavan commented May 18, 2020

@jpaquim would you mind trying out #109?

@jpaquim
Copy link
Author

jpaquim commented May 19, 2020

@ctavan thank you for the cleanup!

Your solution #109 is working correctly here, displaying the warning as expected.

Just a question, is the warning actionable in any way? I guess it doesn't really make sense to push maintainers for every package using "exports" to also export the package.json file, right?

ctavan added a commit to ctavan/rollup-plugin-svelte that referenced this pull request May 19, 2020
@ctavan
Copy link

ctavan commented May 19, 2020

@jpaquim thanks for testing. You are right: Right now the svelte rollup plugin can't really know if a package author just didn't expose package.json by accident or deliberately.

In the case of uuid for example, we currently deliberately don't expose the package.json and there's also no svelte-specific config in it.

I'm not sure how to make this more actionable. You may want to watch nodejs/node#33460 where we are trying to fix the root cause of this whole issue rather than providing workarounds like #109.

@jpaquim
Copy link
Author

jpaquim commented May 19, 2020

@ctavan thanks for the pointer to the upstream issue.

I guess my main doubt right now is that, in the situation where a package deliberately does not expose the package.json (as is apparently the case with uuid), does it make sense to issue a warning on every svelte rollup build that uses that package?

Assuming that node itself doesn't change its behavior with regards to how require.resolve() behaves, I expect there to be a huge number of these warnings (as they have a tendency to grow over time), false positives if you will. Then again, not issuing the warning can lead to really hard to debug scenarios, I think it may be the best approach for now, and let's watch the upstream node issue.

Thanks for all your work looking into this 👍

@benmccann
Copy link
Member

Thanks for proposing a fix for this! I'm going to close it in favor of #119, which I think we're leaning towards as a solution

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

3 participants