next.js: Error occurred prerendering page "/_not-found" with pageExtensions option

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/misty-glade-yh92cc

To Reproduce

After update to 14.2.* version, when i use pageExtensions in next.config, i got an error while try to build my app

module.exports = {
  pageExtensions: ['page.tsx', 'page.ts'],
}

i have in my app folder all files with .page.tsx -

app:
- error.page.tsx
- global-error.page.tsx
- not-found.page.tsx
- layout.page.tsx
- page.page.tsx

Current vs. Expected behavior

Build app fail with pageExtentions option

✓ Linting and checking validity of types    
 ✓ Collecting page data    
   Generating static pages (0/8)  [=   ]
Error occurred prerendering page "/_not-found". Read more: https://nextjs.org/docs/messages/prerender-error

TypeError: Cannot read properties of undefined (reading 'clientModules')
    at /home/ui/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:23603
    at /home/ui/node_modules/next/dist/server/lib/trace/tracer.js:191:62
    at /home/ui/node_modules/next/dist/server/lib/trace/tracer.js:140:36
    at NoopContextManager.with (/home/ui/node_modules/@opentelemetry/api/build/src/context/NoopContextManager.js:25:19)
    at ContextAPI.with (/home/node_modules/@opentelemetry/api/build/src/api/context.js:60:46)
    at NoopTracer.startActiveSpan (/home/ui/node_modules/@opentelemetry/api/build/src/trace/NoopTracer.js:65:31)
    at ProxyTracer.startActiveSpan (/home/ui/node_modules/@opentelemetry/api/build/src/trace/ProxyTracer.js:36:24)
    at /home/ui/node_modules/next/dist/server/lib/trace/tracer.js:122:103
    at NoopContextManager.with (/home/ui/node_modules/@opentelemetry/api/build/src/context/NoopContextManager.js:25:19)
    at ContextAPI.with (/home/ui/node_modules/@opentelemetry/api/build/src/api/context.js:60:46)
 ✓ Generating static pages (8/8)

> Export encountered errors on following paths:
	/_not-found/page: /_not-found

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #29~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr  4 14:39:20 UTC 2
  Available memory (MB): 15622
  Available CPU cores: 12
Binaries:
  Node: 20.12.2
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.2.3
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.5
Next.js Config:
  output: N/A

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

Not sure

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

next build (local)

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 32
  • Comments: 65

Commits related to this issue

Most upvoted comments

From my research, it seems that the only workaround is to use “not-found.ts” or “not-found.tsx”.

Briefly, the workaround is to use the file not-found.ts instead of not-found<pageExtensions> even when using custom pageExtensions.

  1. add "ts" to "pageExtensions" option
  2. use not-found.ts instad of not-found<pageExtensions> file
/** @type {import("next").NextConfig} */
const nextConfig = {
    reactStrictMode: true,
    pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],
};
module.exports = nextConfig;

📝 Probably, You can also work around this by using pageExtensions: [... "tsx"] and not-found.tsx. However, users of pageExtensions will want to avoid the combination of pageExtensions: [“page.tsx”, “tsx”][^1], so the example is written with pageExtensions: [..., "ts"].

Example:

This example is based on @riccardolardi reproduce repo

However, I believe this behavior is a bug because it is inconsistent. I expect it to be corrected for this reason.

[^1]: The combination of “page.tsx” and “tsx” is likely to generate a “Duplicate page detected.” warning, and I not want to specify these into the pageExtensions option at the same time.

experiencing same issue after upgrade to 15-rc

guys i spent an hour on this bug

my issue was that i didn’t have my ENVs set up in vercel

leaving this here in case it helps someone

I have the same issue, builds fine locally but on Vercel I get this error:

[20:37:58.035] Running build in Washington, D.C., USA (East) – iad1
[20:37:59.058] Cloning github.com/riccardolardi/maybeparis-next (Branch: main, Commit: 2875c34)
[20:37:59.894] Cloning completed: 835.705ms
[20:38:04.646] Restored build cache
[20:38:04.883] Running "vercel build"
[20:38:05.934] Vercel CLI 34.1.10
[20:38:06.673] Installing dependencies...
[20:38:07.448] yarn install v1.22.19
[20:38:07.646] [1/4] Resolving packages...
[20:38:08.138] [2/4] Fetching packages...
[20:38:08.146] warning Pattern ["strip-ansi-cjs@npm:strip-ansi@^6.0.1"] is trying to unpack in the same destination "/vercel/.cache/yarn/v6/npm-strip-ansi-cjs-6.0.1-9e26c63d30f53443e9489495b2105d37b67a85d9-integrity/node_modules/strip-ansi-cjs" as pattern ["strip-ansi@^6.0.1"]. This could result in non-deterministic behavior, skipping.
[20:38:08.146] warning Pattern ["string-width@^4.1.0"] is trying to unpack in the same destination "/vercel/.cache/yarn/v6/npm-string-width-cjs-4.2.3-269c7117d27b05ad2e536830a8ec895ef9c6d010-integrity/node_modules/string-width-cjs" as pattern ["string-width-cjs@npm:string-width@^4.2.0"]. This could result in non-deterministic behavior, skipping.
[20:38:08.146] warning Pattern ["strip-ansi@^6.0.0"] is trying to unpack in the same destination "/vercel/.cache/yarn/v6/npm-strip-ansi-cjs-6.0.1-9e26c63d30f53443e9489495b2105d37b67a85d9-integrity/node_modules/strip-ansi-cjs" as pattern ["strip-ansi@^6.0.1"]. This could result in non-deterministic behavior, skipping.
[20:38:32.651] [3/4] Linking dependencies...
[20:38:32.652] warning " > urql@4.1.0" has unmet peer dependency "@urql/core@^5.0.0".
[20:38:37.594] [4/4] Building fresh packages...
[20:38:37.611] Done in 30.17s.
[20:38:37.660] Detected Next.js version: 14.2.3
[20:38:37.662] Running "yarn run build"
[20:38:37.869] yarn run v1.22.19
[20:38:37.892] $ next build
[20:38:38.676]   ▲ Next.js 14.2.3
[20:38:38.676] 
[20:38:38.751]    Creating an optimized production build ...
[20:38:52.305]  ✓ Compiled successfully
[20:38:52.306]    Linting and checking validity of types ...
[20:38:59.325]    Collecting page data ...
[20:38:59.842] TypeError [ERR_INVALID_URL]: Invalid URL
[20:38:59.842]     at new NodeError (node:internal/errors:405:5)
[20:38:59.842]     at new URL (node:internal/url:676:13)
[20:38:59.843]     at 6192 (/vercel/path0/.next/server/app/_not-found/page.js:1:4320)
[20:38:59.843]     at t (/vercel/path0/.next/server/webpack-runtime.js:1:128)
[20:38:59.843]     at 338 (/vercel/path0/.next/server/app/_not-found/page.js:1:954)
[20:38:59.843]     at t (/vercel/path0/.next/server/webpack-runtime.js:1:128)
[20:38:59.844]     at n (/vercel/path0/.next/server/app/_not-found/page.js:43:75)
[20:38:59.848]     at /vercel/path0/.next/server/app/_not-found/page.js:43:106
[20:38:59.848]     at t.X (/vercel/path0/.next/server/webpack-runtime.js:1:1206)
[20:38:59.848]     at /vercel/path0/.next/server/app/_not-found/page.js:43:88 {
[20:38:59.848]   input: 'undefined',
[20:38:59.848]   code: 'ERR_INVALID_URL'
[20:38:59.849] }
[20:38:59.849] 
[20:38:59.849] > Build error occurred
[20:38:59.849] Error: Failed to collect page data for /_not-found
[20:38:59.849]     at /vercel/path0/node_modules/next/dist/build/utils.js:1268:15
[20:38:59.849]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[20:38:59.849]   type: 'Error'
[20:38:59.849] }
[20:38:59.887] error Command failed with exit code 1.
[20:38:59.888] info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[20:38:59.927] Error: Command "yarn run build" exited with 1
[20:39:00.377] 

I was able to fix this issue by padding in pageExtension to the nextConfig.

 pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],

Here’s my full next.config.ts

import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  experimental: {
    ppr: true,
  },
  pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],
  images: {
    remotePatterns: [
      {
        hostname: 'avatar.vercel.sh',
      },
    ],
  },
  // Add configuration for Auth.js trusted hosts
  serverRuntimeConfig: {
    auth: {
      trustHost: true,
    },
  },
};

export default nextConfig;

Using “next”: “15.3.0-canary.31” btw

The build works but then all my pages starts throwing 404 not found 😕

I also had this issue and i think i managed to fix it this morning. I’m using next-intl as well (like few other people here) and fix for me was turning not-found.tsx page into server component and adding “force-dynamic” on top:

export const dynamic = "force-dynamic";
import { redirect } from "next/navigation";

export default function NotFound() {
  redirect("/dashboard");
  return <div></div>;
}

I also added unstable_setRequestLocale(params.locale); to all layout and page components which are server components. That alone didn’t help, but the above mentioned solution did. Hope it helps someone!

My next.config.mjs file looks like this:

import createNextIntlPlugin from "next-intl/plugin";

const withNextIntl = createNextIntlPlugin();

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "standalone",
  reactStrictMode: true,
};

export default withNextIntl(nextConfig);

Another thing I found — after running a build, looking at the .next/app-paths-manifest.json file, I see an entry for "/_not-found/page": "app/_not-found/page.js". I also noticed that for every other entry in there, there is a corresponding <bundlename>-client-reference-manifest.js, but this file is missing for the not-found page.

# notice that a file shows up for every page entry
# but is missing for the not-found page
find .next/ -path '*client-reference-manifest.js'

I’m guessing that this missing file corresponds to an undefined clientReferenceManifest I mentioned in my last comment.

When I downgrade to v14.1.4, I see a couple of differences:

  • The not-found page is built to .next/server/app/_not-found.js (instead of .next/server/app/_not-found/page.js in v14.2.3)
  • The file .next/server/app/_not-found_client-reference-manifest.js exists

I now think this bug was caused by https://github.com/vercel/next.js/pull/62679

I was able to fix this issue by padding in pageExtension to the nextConfig.

 pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],

Here’s my full next.config.ts

import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  experimental: {
    ppr: true,
  },
  pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],
  images: {
    remotePatterns: [
      {
        hostname: 'avatar.vercel.sh',
      },
    ],
  },
  // Add configuration for Auth.js trusted hosts
  serverRuntimeConfig: {
    auth: {
      trustHost: true,
    },
  },
};

export default nextConfig;

Using “next”: “15.3.0-canary.31” btw

I was able to fix this issue by padding in pageExtension to the nextConfig.

 pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],

Here’s my full next.config.ts

import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  experimental: {
    ppr: true,
  },
  pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],
  images: {
    remotePatterns: [
      {
        hostname: 'avatar.vercel.sh',
      },
    ],
  },
  // Add configuration for Auth.js trusted hosts
  serverRuntimeConfig: {
    auth: {
      trustHost: true,
    },
  },
};

export default nextConfig;

Using “next”: “15.3.0-canary.31” btw

This first seemed to work as a workaround for me, but then I ran into other issues. The final solution for my problem was this: https://github.com/wpengine/faustjs/issues/361#issuecomment-885017853 I’m using next v15.3.0 and app routing.

I found a fix that worked for me. If you’re using i18Next for translation make sure the provider is on the client side
change my import from : import { I18nextProvider } from “react-i18next”;

to: const I18nextProvider = dynamic( () => import(“react-i18next”).then((mod) => mod.I18nextProvider), { ssr: false, } );

and it fixed my issue

Alright i think i found the issue. What worked for me so far was to rename the default function in this file /src/app/not-found.tsx from NotFoundPage to NotFound

From my research, it seems that the only workaround is to use “not-found.ts” or “not-found.tsx”.

Briefly, the workaround is to use the file not-found.ts instead of not-found<pageExtensions> even when using custom pageExtensions.

  1. add "ts" to "pageExtensions" option
  2. use not-found.ts instad of not-found<pageExtensions> file
/** @type {import("next").NextConfig} */
const nextConfig = {
    reactStrictMode: true,
    pageExtensions: [
        "page.tsx",
        "page.ts",
        // FIXME: Next.js has a bug which does not resolve not-found.page.tsx correctly
        // Instead, use `not-found.ts` as a workaround
        // "ts" is required to resolve `not-found.ts`
        // https://github.com/vercel/next.js/issues/65447
        "ts"
    ],
};
module.exports = nextConfig;

📝 Probably, You can also work around this by using pageExtensions: [... "tsx"] and not-found.tsx. However, users of pageExtensions will want to avoid the combination of pageExtensions: [“page.tsx”, “tsx”]1, so the example is written with pageExtensions: [..., "ts"].

Example:

This example is based on @riccardolardi reproduce repo

However, I believe this behavior is a bug because it is inconsistent. I expect it to be corrected for this reason.

Footnotes

  1. The combination of “page.tsx” and “tsx” is likely to generate a “Duplicate page detected.” warning, and I not want to specify these into the pageExtensions option at the same time. ↩

I thought this had solved my building issue, as in the fact the build finishes successfully. However, if I try to run that build locally (or deploy it), all routes render the default 404 not found page.

If I remove that next.config.mjs fix, I can run my app flawlessly, but then again, I can’t build it.

Without next.config.mjs patch

runtime ✅ build ❌

Screenshot 2024-09-08 at 11 51 01
With next.config.mjs patch
  • runtime ❌
  • build ✅
Screenshot 2024-09-08 at 11 47 04
Current folder structure runtime ✅ build ❌ Screenshot 2024-09-08 at 11 54 23

Did anyone actually overcome this issue?

Another thing I found — after running a build, looking at the .next/app-paths-manifest.json file, I see an entry for "/_not-found/page": "app/_not-found/page.js". I also noticed that for every other entry in there, there is a corresponding <bundlename>-client-reference-manifest.js, but this file is missing for the not-found page.

# notice that a file shows up for every page entry
# but is missing for the not-found page
find .next/ -path '*client-reference-manifest.js'

I’m guessing that this missing file corresponds to an undefined clientReferenceManifest I mentioned in my last comment.

When I downgrade to v14.1.4, I see a couple of differences:

* The not-found page is built to `.next/server/app/_not-found.js` (instead of `.next/server/app/_not-found/page.js` in v14.2.3)

* The file `.next/server/app/_not-found_client-reference-manifest.js` exists

I now think this bug was caused by #62679

Downgraded to 14.1.4 from my package.json, and I can confirm I no longer see the following error:

> Export encountered errors on following paths:
	/_not-found/page: /_not-found

What worked for me was using my .env file instead of .env.local. At first I had only .env.local where all my variables were, but then I made .env and placed my variables in that. Now it builds fine.

My version of Nexjts is 15.3.1 plus I am also using supabase too "@supabase/ssr": "^0.5.2","@supabase/supabase-js": "^2.50.0",

node v22.14.0 may produce this error while v20.14.0 can build successfully.

Same here, I’m now able to make it work with node v22.14.0 with the pageExtensionssolution, as @kaisewhite suggested

node v22.14.0 may produce this error while v20.14.0 can build successfully.

I’m using the following dependencies in my Next.js project:

  • Next.js: "next": "15.1.0"
  • React: "react": "^19.0.0"
  • Package Manager: pnpm 9.3.0

I ran pnpm build but can confirm that the issue still persists.

Project Setup

page.tsx

export default async function Page() {
  return <div>Page</div>;
}

next.config.mjs

/** @type {import('next').NextConfig} */
const nextConfig = {
  pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'page.tsx', 'page.ts'],
  reactStrictMode: false,
};

export default nextConfig;

Additional Debugging Steps Taken

I have tried the following but the issue remains:

  • Adding output: 'standalone' to nextConfig
  • Adding export const dynamic = 'force-dynamic' to page.tsx

Despite these adjustments, the error persists during the build process. Any insights or suggestions would be greatly appreciated.

Image

@ericpark11 - Did you ever find a solution to this? I just installed next-intl into a new project and am facing the same issue. Thanks.

Downgrading to the latest version of Next 14 (from Next 15) completely fixed things for me.

I faced this while using next-auth and not providing NEXTAUTH_URL env. Local build was working fine, but the Vercel cloud build was failing. Resolved after setting this environment variable.

Details on https://next-auth.js.org/configuration/options.

We faced the issue because we used i18n / react-intl to have a localized not found page. Removal of the localization solved this issue for us.

This issue also can happen when you have circular dependencies in the code even if you don’t have not-found page in the codebase, the error still references /_not-found.

Since today the build suddenly succeeds… must’ve been something related to Vercel?

What version is working for you?

I think my problem was that I was having page.tsx inside an (index) folder. Taking it out there solved the issue if I remember correctly

thank you for the quick reply. I don’t have my index page inside a folder. But again would help to know what version you’re using of 14 or if it’s a canary of 15?

I’m using latest 14

@riccardolardi So 14.2.4 and are you using page extensions like OP?

Yes, but no page extensions

Since today the build suddenly succeeds… must’ve been something related to Vercel?

What version is working for you?

I think my problem was that I was having page.tsx inside an (index) folder. Taking it out there solved the issue if I remember correctly

thank you for the quick reply. I don’t have my index page inside a folder. But again would help to know what version you’re using of 14 or if it’s a canary of 15?

I’m using latest 14

Since today the build suddenly succeeds… must’ve been something related to Vercel?

What version is working for you?

I think my problem was that I was having page.tsx inside an (index) folder. Taking it out there solved the issue if I remember correctly