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

"Error: Cannot read properties of null (reading 'useContext')" in standalone server #51482

Closed
1 task done
shehi opened this issue Jun 19, 2023 · 9 comments · Fixed by #51506 or #51611
Closed
1 task done

"Error: Cannot read properties of null (reading 'useContext')" in standalone server #51482

shehi opened this issue Jun 19, 2023 · 9 comments · Fixed by #51506 or #51611
Labels
bug Issue was opened via the bug report template. locked

Comments

@shehi
Copy link

shehi commented Jun 19, 2023

Verify canary release

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

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #82-Ubuntu SMP Tue Jun 6 23:10:23 UTC 2023
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.7-canary.1
      eslint-config-next: 13.4.6
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

N/A

To Reproduce

Unfortunately I can't share any code - my app is not open source.

I can give some points tho:

  1. Not using next/image , Image Optimization.
  2. Output = "standalone" (makes deployments smaller).
  3. As you can see, apparently (somehow) error is happening inside SWRConfig. My layout and all providers are wrapped by SWRConfig.
  4. Using React.Suspense almost everywhere I need suspense. Only 2 components use next/dynamic. Pretty sure these 2 components have no relation to the issue at hand.

Describe the Bug

While my whole app is working quite flawlessly in NextJs v13.4.5, it broke once I upgraded to v13.4.6. The same error continues in the latest canary.

Following error started to popup across board:

TypeError: Cannot read properties of null (reading 'useContext')
    at exports.useContext (/var/www/.next/standalone/node_modules/react/cjs/react.production.min.js:24:118)
    at SWRConfig (file:///var/www/.next/standalone/node_modules/swr/_internal/dist/index.mjs:519:26)
    at Ge (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:114:273)
    at Z (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)
    at Ge (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:115:9)
    at Z (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)
    at Ge (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:119:95)
    at Z (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)
    at Ge (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:115:9)
    at Z (/var/www/.next/standalone/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)

This is happening only on production and only when I run app via node server.js instead of npm start (standalone output recommends the former). During npm run dev or npm start everything works fine.

Expected Behavior

As in v13.4.5, I shouldn't be getting the aforementioned errors which only happen in v13.4.6.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

node start.js (standalone output)

@shehi shehi added the bug Issue was opened via the bug report template. label Jun 19, 2023
@shuding shuding changed the title v13.4.6 started causing Error: Cannot read properties of null (reading 'useContext') "Error: Cannot read properties of null (reading 'useContext')" in standalone server Jun 19, 2023
@shuding
Copy link
Member

shuding commented Jun 19, 2023

Seems to be caused by the changes in #51172.

@huozhi
Copy link
Member

huozhi commented Jun 20, 2023

@shehi Are you seeing errors in pages/ or app/?

@alexanbj
Copy link

alexanbj commented Jun 20, 2023

@huozhi I just encountered this bug, in an app using pages. Downgrading to 13.4.5 fixed the issue for me.

edit:

Added stacktrace

023-06-20T12:34:21.970905674Z: [ERROR]  TypeError: Cannot read properties of null (reading 'useContext')
2023-06-20T12:34:21.970909174Z: [ERROR]      at exports.useContext (/home/site/wwwroot/node_modules/react/cjs/react.production.min.js:24:118)
2023-06-20T12:34:21.970912574Z: [ERROR]      at file:///home/site/wwwroot/node_modules/@obosbbl/grunnmuren-react/dist/grunnmuren.mjs:214:14
2023-06-20T12:34:21.970915974Z: [ERROR]      at Ge (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:118:245)
2023-06-20T12:34:21.970923174Z: [ERROR]      at Z (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)
2023-06-20T12:34:21.970926874Z: [ERROR]      at Ge (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:115:9)
2023-06-20T12:34:21.970930374Z: [ERROR]      at Z (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)
2023-06-20T12:34:21.970933874Z: [ERROR]      at He (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:123:155)
2023-06-20T12:34:21.970937374Z: [ERROR]      at Je (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:122:100)
2023-06-20T12:34:21.970940774Z: [ERROR]      at Z (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:222)
2023-06-20T12:34:21.970944274Z: [ERROR]      at He (/home/site/wwwroot/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:123:155)

@huozhi
Copy link
Member

huozhi commented Jun 20, 2023

@alexanbj Thanks for provding the info, will investigate it asap

@Breuls
Copy link

Breuls commented Jun 20, 2023

I have the same problem, also solved by downgrading to 13.4.5. If another stacktrace helps, I'd be happy to post it, but it's basically the same as above, except for the third line (which refers to a different dependency).

@shehi
Copy link
Author

shehi commented Jun 20, 2023 via email

@huozhi
Copy link
Member

huozhi commented Jun 20, 2023

Can't repro that easily with simple setup with SWR SSR in pages/ folder and standalone. Does anyone has a minimal reproduction how it fails?

@Breuls
Copy link

Breuls commented Jun 21, 2023

I've got one right here: https://github.com/Breuls/next-issue-51482

It looks like the minimal steps I needed were:

  1. Use the useTranslation hook
  2. Use a getServerSideProps function

Even though useTranslation is the next step in the error trace, I needed the SSP function to actually trigger it.

ijjk pushed a commit that referenced this issue Jun 21, 2023
kodiakhq bot pushed a commit that referenced this issue Jun 22, 2023
Request data flow in the server

```
request ---> router worker (1) ---> ipc ---> render worker app (2)
                                                              |-----> render worker pages (3)
```

When it's hitting `_next/*` unmatched routes in standalone server, it will render 404, but when you hit `_next/*` it will render app not-found as the app router is always enabled, but router worker isn't set up with require-hook for proper built-in react version, then the app-render will fail with `./server.edge` exports not found error.

We detect if it's in the render worker, then do the app paths rendering instead of directly looking up for app paths in route worker. This could avoid unexpected accesses to the app-render

Fixes #51482 
Fixes #50232
Closes #51506
Reverts changes in #51172
fix NEXT-1260
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
5 participants