server-only causes errors when generating types, migrations, or sourceMaps
Link to reproduction
https://github.com/HarleySalas/payload-3-reproductions/tree/server-only-error
Payload Version
beta.91
Node Version
22.2.0
Next.js Version
canary-104
Describe the Bug
server-only is a best practice for ensuring code is only run on the server.
This issue was previously resolved in #6383, however it has resurfaced, I assume in beta.79.
Reproduction Steps
Use a function with import 'server-only' inside of the payload config somewhere(?)
In my case, its for a custom auth strategy, but I'd assume its causing errors anywhere that it'd be used.
Then, try payload generate:types, payload migrate:create, etc.
Adapters and Plugins
n/a
I guess even if it is best practice it's not required anymore now that payload config is server only from beta.79 onwards.
To mitigate this you could add this dependency https://www.npmjs.com/package/server-only as far as i know.
Even if we call any nextjs api where "server-only" is used the type generation doesn't work. We may call those apis in the hooks of the collection.
@jmikrut please let me know if u have any better way of handling this issue
This issue has been marked as stale due to lack of activity.
To keep this issue open, please indicate that it is still relevant in a comment below.
This issue was automatically closed due to lack of activity.
Is this still an issue? Should you even need to add server-only? The payload config cannot even send functions to the client.
There are situations where you may have a function that is also used elsewhere outside of payload hooks, and this function should only ever be used on the server so you'd like to protect it with 'server-only' but that's not currently possible because it breaks payload type generation.
I don't think closing this issue is the right call, type generation shouldn't suddenly break because of the presence of 'server-only'.
This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.