next.js: Build error on Vercel deployments after upgrading to 13.4

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 Version 22.4.0: Mon Mar  6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64
    Binaries:
      Node: 16.13.2
      npm: 9.6.4
      Yarn: 3.4.1
      pnpm: 7.21.0
    Relevant packages:
      next: 13.4.0
      eslint-config-next: 13.4.0
      react: 18.2.0
      react-dom: 18.2.0

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

No response

Link to the code that reproduces this issue

none

To Reproduce

Upgrade next from ^13.3.4 tp ^13.4.0. The project is using pages/, not using app/dir nor turbopack.

Describe the Bug

I just updated the nextJS version from 13.3 to 13.4 without change anything in my codebase. The local build is successful without any errors, but when deployed on Vercel, I got hundreds of lines of error like

Error occurred prerendering page "/en/post/100". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read properties of null (reading 'useEffect')
    at exports.useEffect (/vercel/path0/node_modules/react/cjs/react.production.min.js:24:292)
    at Analytics (file:///vercel/path0/node_modules/@vercel/analytics/dist/react/index.js:132:3)
    at Ge (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:114:273)
    at Z (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)
    at He (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:123:155)
    at Je (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:122:100)
    at Z (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:222)
    at Ge (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:119:95)
    at Z (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:120:91)
    at Ge (/vercel/path0/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.browser.production.min.js:115:9)

I speculate that this is caused by my components not being specified as client components. But I am not using the app/ directory, I am only using the /pages. Does that mean I need to specify client components even if I am using /pages? I tried adding ‘use client’ to the top of my component files and the error still persists.

The local builds are successful without any errors, this only happens on Vercel deployments.

Expected Behavior

Build should be successful with no error.

Which browser are you using? (if relevant)

irrelevant

How are you deploying your application? (if relevant)

Vercel

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 12
  • Comments: 16 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I was facing a similar issue after trying to upgrade locally for a Next app using a custom server. Adding the following to next.config.js seems to have fixed the issue:

experimental: {
  appDir: false,
},

Have the same issue after upgrade to nextjs 13.4.0 (from 13.3.4) - no app dir - no type module

image

The example repo is:

https://github.com/belgattitude/nextjs-monorepo-example (branch main)

The ci passes (https://github.com/belgattitude/nextjs-monorepo-example/actions/workflows/ci-nextjs-app.yml), vercel just started to break after upgrade.

If you want to try on vercel, you’ll have to add ENABLE_EXPERIMENTAL_COREPACK=1 in the vercel env.

The deployment url: https://monorepo-nextjs-app.vercel.app/

Open to help if needed

PS:

I had also the same issue when I did the migration to Next 13.4.

I added the flag given by @benjwexler , and now it deploy without any issue.

I was facing a similar issue after trying to upgrade locally for a Next app using a custom server. Adding the following to next.config.js seems to have fixed the issue:

experimental: {
  appDir: false,
},

Hey, just upgrade your next package to the latest version or the Canary version. We upgraded to ^13.4.4-canary.1, and everything started working like normal.

It seems like this issue is happening again in 13.4.2. The error message is different tho, this time it’s

Type error: 'MyComponent' cannot be used as a JSX component.
Its element type 'ReactElement<any, any> | Component<{}, any, any> | null' is not a valid JSX element.

Local build is fine but Vercel deployment fails, same as 13.4.0

I am also getting cannot be used as a JSX component. when using a dynamic import.

const Payments = dynamic(() => import("@/components/Account/Payments"), {
  loading: () => <Loading />,
});
...
<AccountLayout curPage={curPage}>
        {
          {
            payments: <Payments />,
            ...
          }[curPage]
        }
      </AccountLayout>

Error:

'Payments' cannot be used as a JSX component.
  Its element type 'ReactElement<any, any> | Component<{}, any, any> | null' is not a valid JSX element.
    Type 'Component<{}, any, any>' is not assignable to type 'Element | ElementClass | null'.
      Type 'Component<{}, any, any>' is not assignable to type 'ElementClass'.
        The types returned by 'render()' are incompatible between these types.
          Type 'React.ReactNode' is not assignable to type 'import(".../node_modules/@types/react-dom/node_modules/@types/react/index").ReactNode'.ts(2786)

We are having the same issue as @MathurAditya724 using NX as well (>=v16). Our error is of the “Cannot read properties of null (reading ‘useEffect’)” variant and we are also using Tanstack Query (which is how I got to this issue). When I downgrade to Next 13.3.4 everything works correctly, but anything past 13.4.x generates the error.

Hey, I am facing the same issue in the latest build of nextjs. I have listed the issues and examples in greater detail on the Tanstack issues, check it out here.

Basically, it is not working in a mono repo created using nx, the build is failing.

Thanks for the config tips, all.

Next needs to add that to the pages docs if it’s now explicitly required to ‘opt out’ of app directory…

https://nextjs.org/docs/pages/api-reference/next-config-js