Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Error [ERR_REQUIRE_ESM]: require() of ES Module error when using component in /pages instead of /app folder #88

Closed
valerioleo opened this issue Nov 3, 2023 · 4 comments
Assignees

Comments

@valerioleo
Copy link

In my project, we trying to integrate next-payload. However we still make use of the traditional /pages folder and we won't be moving those components any time soon.

I have noticed that, differently from components in the /app folder, any component in the /pages folder will break if trying to import a node_module.

I have created a very simple repro here:
payloadcms/next-payload-demo@main...valerioleo:next-payload-demo-fork:main

  1. clone repo
  2. yarn install
  3. yarn dev
  4. navigate to localhost:3000/test-page

I have tried to debug it without success, my hunch is that the use of @swc/register inside the withPayload is complicating things, however I couldn't find a way to fix it yet. I'll keep trying.

@denolfe denolfe self-assigned this Nov 6, 2023
@denolfe
Copy link
Member

denolfe commented Nov 6, 2023

Your problem here is the package you are attempting to use @uidotdev/usehooks is ESM-only and is not able to be used in CJS projects.

@valerioleo
Copy link
Author

as per Discord convo, my nextjs app is a CJS and it was working correctly with ESM modules like @uidotdev/usehooks or superjson (used by tRPC), but it stopped working once I wrapped my next config object with withPayload.

@valerioleo
Copy link
Author

Your problem here is the package you are attempting to use @uidotdev/usehooks is ESM-only and is not able to be used in CJS projects. @denolfe

To further investigate if the issue is a limitation of Payload being a CJS, I have added the @uidotdev/usehooks into a component that is imported using the /app folder. In that case it worked perfectly. You can see it here.

As I see it, this strengthen the thesis that it's withPayload that is messing something up with the transpiration of files in the /pages folder.

@denolfe denolfe assigned JarrodMFlesch and unassigned denolfe Nov 7, 2023
@denolfe
Copy link
Member

denolfe commented Nov 17, 2023

This looks like an issue w/ how yarn v1 resolves dependencies. The workaround is here: payloadcms/payload#4109 (comment)

It sounds like this workaround will work here as well. Let me know if it does not, and I will reopen.

@denolfe denolfe closed this as completed Nov 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants