Open stefanprobst opened 5 months ago
I would also want this.
I'm migrating from pages to app directory and my pages depend on publicRuntimeConfig
. For my publicRuntimeConfig
to work, I'm importing ~/server/env.js
which I would very much like to have import 'server-only'
in now that the boundary between server and client is more fuzzy
@huozhi could you clarify whether this is indeed an issue, or if this is intended behavior? thank you!
ok so the docs state:
next.config.js is a regular Node.js module ... It gets used by the Next.js server and build phases, and it's not included in the browser build.
A slightly hacky workaround I'm using for now:
try {
require("server-only");
} catch (err) {
if (err && typeof err === "object" && "code" in err && err.code === "MODULE_NOT_FOUND") {
} else {
throw err;
}
}
But yea, it would be nice if it just worked.
Edit: My workaround only works if server-only
is not explicitly included in the package.json.
+1 for this feature, I want to mark my server configuration as server only, but I want to check it on build.
Link to the code that reproduces this issue
https://github.com/stefanprobst/issue-next-config-server-import
To Reproduce
pnpm install
pnpm run dev
Current vs. Expected behavior
i am trying to import a module which has
import "server-only";
intonext.config.js
and get the following error:i had assumed that
next.config.js
would not be read client side.i also checked whether
next.config.js
is actually read client-side by adding the following code innext.config.js
(and everything still worked, so i guess importing fromserver-only
modules should be ok):Provide environment information
Which area(s) are affected? (Select all that apply)
Module Resolution
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
also see this discussion: https://github.com/vercel/next.js/discussions/65605#discussioncomment-9393115