Skip to content

Build Failure in Next.js 15.4.5 in turborepo: useContext Throws Null Error During /404 Prerender #10731

@jsdev-robin

Description

@jsdev-robin

Verify canary release

  • I verified that the issue exists in the latest Turborepo canary release.

Link to code that reproduces this issue

https://github.com/vercel/turborepo/tree/main/examples/with-tailwind

Which canary version will you have in your reproduction?

npx create-turbo@latest -e with-tailwind

Environment information

# Output of turbo info
System:
  OS: Windows 10 64-bit
  CPU: Intel Core i7
  Node: 18+
  npm: 9+
  Yarn: Not used
  pnpm: Not used

Packages:
  turbo: latest
  next: 15.4.5
  react: 19+
  typescript: 5+

Expected behavior

The build should complete successfully. Custom _error.tsx or /404 page using useContext should render without throwing null reference errors during next build.

Actual behavior

During the production build (next build), the process fails when attempting to prerender the /404 page.
`TypeError: Cannot read properties of null (reading 'useContext')
at p (....next\server\pages_error.js:19:2590)

Error occurred prerendering page "/404".
Export encountered an error on /_error: /404, exiting the build.
Next.js build worker exited with code: 1 and signal: null
`

To Reproduce

Add a custom _error.tsx or /404.tsx page that uses a React context (e.g., useContext(MyContext))

Run next build inside the root folder

Observe the error: Cannot read properties of null (reading 'useContext')

Additional context

Issue occurs only during production build — works fine in development mode.

The app is part of a Turborepo monorepo setup under /apps/web

Error output points to generated file: .next/server/pages/_error.js

Build environment: Local (Windows), but also reproducible in CI (GitHub Actions)

Custom _error.tsx likely using useContext outside a proper Provider tree at build time

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugSomething isn't workingneeds: triageNew issues get this label. Remove it after triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions