next.js: Unable to build project, Cannot read properties of null (reading 'useContext')

Link to the code that reproduces this issue

https://github.com/zlace/next-build-error

To Reproduce

pnpm dev works, pnpm build fails

https://stackblitz.com/edit/github-cmffvl?file=README.md

Current vs. Expected behavior

Should build successfully

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Thu Sep 14 23:08:13 PDT 2023; root:xnu-10002.40.63.505.1~3/RELEASE_ARM64_T8112
Binaries:
  Node: 18.19.1
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: 8.12.1
Relevant Packages:
  next: 14.1.3
  eslint-config-next: 14.1.1
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Module resolution (CJS / ESM, module resolving)

Which stage(s) are affected? (Select all that apply)

next build (local), Vercel (Deployed)

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 31 (4 by maintainers)

Most upvoted comments

I get this error in dev all the time on dev server with Next 14.2.0 with --turbo, never on build on production. refreshing often fixes it but once it stops working I need to restart the dev server.

Here is the call stack:

t.useContext

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:164822)
useContext

node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/src/client/components/navigation.ts (84:10)
ErrorBoundary

node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/src/client/components/error-boundary.tsx (172:23)
ac

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:10478)
<unknown>

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:15152)
ak

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:16938)
aC

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:49758)
aS

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:11840)
<unknown>

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:16901)
ak

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:16938)
ak

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:46487)
aC

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:49758)
aS

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:11840)
<unknown>

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:16901)
ak

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:16938)
aC

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:49758)
<unknown>

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:58594)
<unknown>

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:59252)
aA

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:59260)
Timeout._onTimeout

file:///Users/omardiab/code/breeze/monorepo/node_modules/.pnpm/next@14.3.0-canary.25_@babel+core@7.23.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:6971)

I’m facing the same issue

Uncaught TypeError: Cannot read properties of null (reading 'useContext')
    at t.useContext (file:///Users/mario/Documents/GitHub/blah/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:162241)
    at usePathname (file:///Users/mario/Documents/GitHub/blahapps/web/.next/server/chunks/ssr/08b5e_next_dist_esm_1c713e._.js:3834:238)
    at ErrorBoundary 

I can reproduce it. Changing the authOptions for next-auth while application is running in dev mode triggers the same error everytime. I always restart the server to fix the problem, It’s extreamly annoying.

Hopefully that will help put someone on the right track to solve it. However, I am not using next-auth, so it’s not limited to just that. But it clearly has something to do with changing some code while in dev mode.

Same kind of issue here, although mine is happening in dev. seems to be coming from serverside. NextJS 14.2.3 with --turbo

Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
 ⨯ TypeError: Cannot read properties of null (reading 'useContext')
    at usePathname (/Users/panda/Documents/Application/frontend/.next/server/chunks/ssr/node_modules_d26df3._.js:4791:238)
    at ErrorBoundary (/Users/panda/Documents/Application/frontend/.next/server/chunks/ssr/node_modules_d26df3._.js:5101:227)
digest: "3330820784"
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
TypeError: Cannot read properties of null (reading 'useContext')
    at t.useContext (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:162241)
    at usePathname (/Users/panda/Documents/Application/frontend/.next/server/chunks/ssr/node_modules_d26df3._.js:4791:238)
    at ErrorBoundary (/Users/panda/Documents/Application/frontend/.next/server/chunks/ssr/node_modules_d26df3._.js:5101:227)
    at au (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:10446)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:15122
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16908)
    at a_ (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:49776)
    at ab (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:11808)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16871
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16908)
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46505)
    at a_ (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:49776)
    at ab (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:11808)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16871
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16908)
    at a_ (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:49776)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:58612
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:59270
    at aN (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:59278)
    at Timeout._onTimeout (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:6939)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
 ⨯ TypeError: Cannot read properties of null (reading 'useContext')
    at t.useContext (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:162241)
    at usePathname (/Users/panda/Documents/Application/frontend/.next/server/chunks/ssr/node_modules_d26df3._.js:4791:238)
    at ErrorBoundary (/Users/panda/Documents/Application/frontend/.next/server/chunks/ssr/node_modules_d26df3._.js:5101:227)
    at au (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:10446)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:15122
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16908)
    at a_ (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:49776)
    at ab (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:11808)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16871
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16908)
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46505)
    at a_ (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:49776)
    at ab (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:11808)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16871
    at aw (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:16908)
    at a_ (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:49776)
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:58612
    at /Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:59270
    at aN (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:59278)
    at Timeout._onTimeout (/Users/panda/Documents/Application/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:6939)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7) {
  page: '/backoffice/leads'
}
 GET /backoffice/leads 500 in 514ms

I still have this problem only in dev mode (14.2.3). This happens on some pages of my application at random times. I will update Next it to 14.2.4 to see if fixes it

have u tried removing the turbo flag if ure using turbo? if you are not using turbo then this issue will haunt us forever. 😁

If I remove turbo the app is so slow that it is better to get random crashes lol

Facing this and been scratching my head for hours.

What seems to work is to run the build with experimental compile mode.

$ npx next build --experimental-build-mode compile

Related comments/issues regarding this flag: https://github.com/vercel/next.js/discussions/46544#discussioncomment-7115457

Extra info:

    "next": "^14.2.3",
    "react": "^18",
    "react-dom": "^18",

I can confirm this run well on my local and production in Vercel. I’m using dynamic rendering anyway since its more of a “CMS” rather than static blog, so this seems fitting in my case.

The same issue happens with my projects that don’t use turbo.

Occurs with a custom development server, > "dev": .env.development.local npm run dev-server and also occurs with npx next dev. > npx next dev

I’m currently using both routers because I’m switching the service from pageRouter to AppRouter.

스크린샷 2024-05-30 09 44 06

My error is

  • This is not a problem when you use next/router or next/navigation to access other routers. - O
  • This is not a problem when you access the path to the page router directly to the url in your browser. - O
  • Problems occur when accessing the route to the app router directly to the url in the browser. - X
  • Problems occur when accessing routes that do not match the router. - X

my version

  • node: 20.9.0
  • next: 14.2.4
  • react: 18.2.0
  • react-dom: 18.2.0

This issue is only comes with --turbo flag. When i don’t use turbo-pack i don’t face the issue. Seems like the turbo pack has issues with other packages.

My versions: next@14.1.4 react@18.2.0 react@18.2.0