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

allow importing "server-only" modules in next.config.js #65652

Open
stefanprobst opened this issue May 11, 2024 · 0 comments
Open

allow importing "server-only" modules in next.config.js #65652

stefanprobst opened this issue May 11, 2024 · 0 comments
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving)

Comments

@stefanprobst
Copy link
Contributor

Link to the code that reproduces this issue

https://github.com/stefanprobst/issue-next-config-server-import

To Reproduce

  1. clone https://github.com/stefanprobst/issue-next-config-server-import and run pnpm install
  2. pnpm run dev
  3. see error message:
 ⨯ Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error

> Build error occurred
Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.

Current vs. Expected behavior

i am trying to import a module which has import "server-only"; into next.config.js and get the following error:

 ⨯ Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error

> Build error occurred
Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.

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 in next.config.js (and everything still worked, so i guess importing from server-only modules should be ok):

if (typeof document !== "undefined") {
  throw new Error("Reading next.config.js on the client.");
}

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #202403151937 SMP PREEMPT_DYNAMIC Fri Mar 15 19:52:22 UTC 2024
  Available memory (MB): 31401
  Available CPU cores: 16
Binaries:
  Node: 20.12.2
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 9.0.6
Relevant Packages:
  next: 14.3.0-canary.57 // Latest available version is detected (14.3.0-canary.57).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: N/A

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: #65605 (comment)

@stefanprobst stefanprobst added the bug Issue was opened via the bug report template. label May 11, 2024
@github-actions github-actions bot added the Module Resolution Module resolution (CJS / ESM, module resolving) label May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving)
Projects
None yet
Development

No branches or pull requests

1 participant