T
TanStack•3y ago
multiple-amethyst

Errors during the build process on vercel

Today around the same time Vercel was doing their new "ship" event. I started getting build errors regarding react query. The errors looked something like this.
TypeError: Cannot read properties of null (reading 'useEffect')
at exports.useEffect (/vercel/path0/node_modules/react/cjs/react.production.min.js:24:292)
at QueryClientProvider (file:///vercel/path0/node_modules/@tanstack/react-query/build/lib/QueryClientProvider.mjs:46:9)
TypeError: Cannot read properties of null (reading 'useEffect')
at exports.useEffect (/vercel/path0/node_modules/react/cjs/react.production.min.js:24:292)
at QueryClientProvider (file:///vercel/path0/node_modules/@tanstack/react-query/build/lib/QueryClientProvider.mjs:46:9)
After talking with Chat GPT it suggested this
The error you're encountering seems to be related to the server-side rendering of the React application. It appears that the useEffect hook is being called on the server-side, which is not supported, as React hooks are intended for client-side rendering only.

To resolve this issue, you can try using the next/dynamic package to dynamically import the QueryClientProvider component, which will defer its rendering to the client-side:

Import the next/dynamic package and use it to dynamically import the QueryClientProvider component:

import React from 'react';
import { QueryClient } from '@tanstack/react-query';
import dynamic from 'next/dynamic';

const queryClient = new QueryClient();

const DynamicQueryClientProvider = dynamic(
() =>
import('@tanstack/react-query').then((mod) => mod.QueryClientProvider),
{ ssr: false }
);

export default function MyApp({ Component, pageProps }) {
return (
<DynamicQueryClientProvider client={queryClient}>
<Component {...pageProps} />
</DynamicQueryClientProvider>
);
}
The error you're encountering seems to be related to the server-side rendering of the React application. It appears that the useEffect hook is being called on the server-side, which is not supported, as React hooks are intended for client-side rendering only.

To resolve this issue, you can try using the next/dynamic package to dynamically import the QueryClientProvider component, which will defer its rendering to the client-side:

Import the next/dynamic package and use it to dynamically import the QueryClientProvider component:

import React from 'react';
import { QueryClient } from '@tanstack/react-query';
import dynamic from 'next/dynamic';

const queryClient = new QueryClient();

const DynamicQueryClientProvider = dynamic(
() =>
import('@tanstack/react-query').then((mod) => mod.QueryClientProvider),
{ ssr: false }
);

export default function MyApp({ Component, pageProps }) {
return (
<DynamicQueryClientProvider client={queryClient}>
<Component {...pageProps} />
</DynamicQueryClientProvider>
);
}
After implementing the dymanic import my builds worked fine. Hope this helps anyone else working on the issue.
6 Replies
conscious-sapphire
conscious-sapphire•3y ago
This effectively disables SSR for your entire application. I'm not sure what the issue is with the info you've given here, but I would advise against that solution for any one who wants to have anything more than a client-side react app
fair-rose
fair-rose•3y ago
Has anyone considered that vercel just screwed something up during their 🚢 event?
conscious-sapphire
conscious-sapphire•3y ago
I'm not sure it's related to the event, but more to the fact that the event reminded ppl to upgrade their package versions. Just a theory, but I had an issue a couple weeks ago on a turborepo monorepo where react wasn't getting bundled correctly and I had to add it as a dependency everywhere (same thing for react-query), whereas it used to work only having it at the root. maybe related? also the issue popped up only when serving a build, no issue on a dev server (but it's a vite project, not next)
fair-rose
fair-rose•3y ago
GitHub
Build error on Vercel deployments after upgrading to 13.4 · Issue #...
Verify canary release I verified that the issue exists in the latest Next.js canary release Provide environment information Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Versi...
conscious-sapphire
conscious-sapphire•3y ago
damn they did mess up lol
judicial-coral
judicial-coral•3y ago
Nice! I had same error, but before the build

Did you find this page helpful?