cli: [Bug]: Cannot read properties of null (reading 'useContext')
Please confirm that you have:
- Searched existing issues to see if your issue is a duplicate. (If you’ve found a duplicate issue, feel free to add additional information in a comment on it.)
- Reproduced the issue in the latest CLI version.
In which of these areas are you experiencing a problem?
App, Extension
Expected behavior
When running shopify app dev it should run the application.
Actual behavior
The app does not run and instead outputs this error
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.
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.
ERROR Cannot read properties of null (reading 'useContext')
node_modules/@shopify/app/node_modules/react/cjs/react.development.js:1618:21
- useContext (node_modules/@shopify/app/node_modules/react/cjs/react.development.js:1618:21)
- useStdin (node_modules/@shopify/app/node_modules/ink/build/hooks/use-stdin.js:6:24)
- Dev (node_modules/@shopify/app/dist/cli/services/dev/ui/components/Dev.js:14:53)
- renderWithHooks (node_modules/@shopify/cli-kit/node_modules/react-reconciler/cjs/react-reconciler.development.js:7478:18)
- mountIndeterminateComponent (node_modules/@shopify/cli-kit/node_modules/react-reconciler/cjs/react-reconciler.development.js:11247:13)
- beginWork (node_modules/@shopify/cli-kit/node_modules/react-reconciler/cjs/react-reconciler.development.js:12760:16)
- beginWork$1 (node_modules/@shopify/cli-kit/node_modules/react-reconciler/cjs/react-reconciler.development.js:19569:14)
- performUnitOfWork (node_modules/@shopify/cli-kit/node_modules/react-reconciler/cjs/react-reconciler.development.js:18703:12)
- workLoopSync (node_modules/@shopify/cli-kit/node_modules/react-reconciler/cjs/react-reconciler.development.js:18609:5)
- renderRootSync (node_modules/@shopify/cli-kit/node_modules/react-reconciler/cjs/react-reconciler.development.js:18577:7)
Reproduction steps
- Install the lastest cli version (this bug appears on 3.49.X)
- Simplified deployments is enabled
- Run
shopify app dev
Operating System
WSL 2 (Ubuntu 18). Chrome
Shopify CLI version (check your project’s package.json if you’re not sure)
3.49.2
Shell
No response
Node version (run node -v if you’re not sure)
16.17.0
What language and version are you using in your application?
Node 16
About this issue
- Original URL
- State: closed
- Created 10 months ago
- Reactions: 1
- Comments: 38 (15 by maintainers)
I’m still very much confused on what the approach should be here?
POS UI extensions from
@shopify/retail-ui-extensions-reactutilize@remote-ui/reactlocked to4.5.x, which doesn’t support React 18 yet, yet the CLI is instructing React 18 as a dependency. Should we be sticking to an old version of the CLI until React 18 is supported downtream in the dependency tree?Upgrading React from
17.0to18.2.0solved this issue for me.For other folks having this issue, another workaround would be to remove react 17 from the top level package.json, remove node_modules AND remove package-lock.json.
Hey folks, I’ve released
3.49.3which should fix the issue going forward. If you have a broken app, these are the steps to fix it:@shopify/appto3.49.3in your top-levelpackage.json@shopify/clito3.49.3in your top-levelpackage.jsonreactfrom your top-levelpackage.jsonrm package-lock.json && rm -rf node_modules && npm installNow
npm run devshould work again. Thanks for reporting!Ha, I have the same issue here. It just not working even the “sample” code are generated from the CLI.
I solved the issue by adding a
package.jsonfile in theextension\[MY EXTENSION NAME]folder with the following dependencies:@matteodepalo The issue happens in a newly created project when running
shopify app dev.The following a included in at the top level package.json
These are the warnings when installing the package.
fyi. The issue only happened in `yarn dev’. It works in ‘npm run dev’.
Great to hear! Workspaces is used by our latest templates and it should improve the dependency management situation among all the sub-folders. All new templates include this at the top level package.json
That said if the current situation works for you there is no rush to change, we’ll keep supporting projects with and without workspaces.
yes and removing react from the root package.json, changing versions of shopify CLI, using different versions of node etc
@matteodepalo I checked and it says
"^3.49.3". I have found a (somewhat lengthy) workaround for now:--legacy-peer-depsflagpackage.jsonto the newly generated extension directory, containingreact,typescript,@shopify/ui-extensions, and@shopify/ui-extensions-reactpackages.npm remove react @shopify/ui-extensions @shopify/ui-extensions-reactfrom the root directory to remove the packages that were added to the rootpackage.jsonnpm updateto re-install the react version required by the cli/ink packagesnpm run dev, works correctly nowIn addition to what Matteo said, we’ve added workspaces support many months ago, so you can specify the UI extension dependency (such as react) inside the specific
package.jsonfor your extension. Try creating a new app from scratch in this way:npm init @shopify/app@latestnpm run generate extension(choose checkout UI)extensions/my-checkout-ui/package.jsonthat only contains the dependencies of your extensionThen you can adapt the changes into your existing app, they should be fairly straightforward.
React at the top level is actually not needed, it was added because of a quirk with
npm, but if you upgrade to the latest version it won’t be added anymore when you generate a new UI extension.@Arkham I think you closed this too soon. It’s unreasonable to have the fix dependant on not having react installed in the top level
package.json, especially as that is the happy path for for things like UI extensions.