payload icon indicating copy to clipboard operation
payload copied to clipboard

server-only causes errors when generating types, migrations, or sourceMaps

Open HarleySalas opened this issue 1 year ago • 3 comments

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

HarleySalas avatar Aug 25 '24 10:08 HarleySalas

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.

yobottehg avatar Aug 27 '24 16:08 yobottehg

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

Aqib-Rime avatar Nov 12 '24 06:11 Aqib-Rime

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.

github-actions[bot] avatar Dec 13 '24 05:12 github-actions[bot]

This issue was automatically closed due to lack of activity.

github-actions[bot] avatar Dec 21 '24 05:12 github-actions[bot]

Is this still an issue? Should you even need to add server-only? The payload config cannot even send functions to the client.

JarrodMFlesch avatar Jul 15 '25 18:07 JarrodMFlesch

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'.

rilrom avatar Jul 15 '25 20:07 rilrom

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Jul 23 '25 05:07 github-actions[bot]