next.js: Error: should not be imported outside of pages/_document.

Link to the code that reproduces this issue

https://github.com/kobimantzur/next-error

To Reproduce

create-next-app is broken.

  1. npx create-next-app@latest test
  2. Enter the generated folder
  3. npm run build

Current vs. Expected behavior

Expected result: build should complete Actual result:

➜  web git:(main) ✗ npm run build

> app@0.1.0 build
> PORT=4001 next build

 ⚠ You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env
 ✓ Creating an optimized production build
 ✓ Compiled successfully
   Skipping linting
 ✓ Checking validity of types
 ✓ Collecting page data
   Generating static pages (0/2)  [    ]Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/private/var/ski/web/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:38:4358)
    at Html (/private/var/ski/web/.next/server/chunks/241.js:6:1327)
    at renderWithHooks (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at finishClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5688:3)
    at renderClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:3)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:7)

Error occurred prerendering page "/404". Read more: https://nextjs.org/docs/messages/prerender-error
Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/private/var/ski/web/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:38:4358)
    at Html (/private/var/ski/web/.next/server/chunks/241.js:6:1327)
    at renderWithHooks (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at finishClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5688:3)
    at renderClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:3)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:7)
Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/private/var/ski/web/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:38:4358)
    at Html (/private/var/ski/web/.next/server/chunks/241.js:6:1327)
    at renderWithHooks (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at finishClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5688:3)
    at renderClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:3)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:7)

Error occurred prerendering page "/500". Read more: https://nextjs.org/docs/messages/prerender-error
Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/private/var/ski/web/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:38:4358)
    at Html (/private/var/ski/web/.next/server/chunks/241.js:6:1327)
    at renderWithHooks (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at finishClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5688:3)
    at renderClassComponent (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5696:3)
    at renderElement (/private/var/ski/web/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5943:7)
 ✓ Generating static pages (2/2)

> Export encountered errors on following paths:
	/_error: /404
	/_error: /500
➜  web git:(main) ✗

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
Binaries:
  Node: 18.12.1
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: 7.9.5
Relevant Packages:
  next: 13.5.4
  eslint-config-next: 13.5.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

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

CLI (create-next-app)

Additional context

No response

NEXT-1709

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Reactions: 15
  • Comments: 24 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I am also facing this issue. I was initially getting errors regarding NextRouter not mounted and tried to narrow down the root cause and eventually came across this issue. Both NextRouter not mounted and <Html> should not be imported outside of pages/_document were followed by Error occurred prerendering page...

Note: I’ve tried using npm instead of pnpm, but the issue persists. For me, it seems to be happening only in non-Windows environments. (It builds successfully on Windows).

Lastly, it seems that I’m also getting You are using a non-standard "NODE_ENV" value in your environment. (despite setting it to either production or development in my Dockerfile. Is this related?

I had exactly the same errors, and after removing NODE_ENV=development from my local enviroment (which I did manually set), everything is working again.

I don’t know what kind of dark magic is going on behind, but the error messages are not helpful at all. Thank you for pointing that out with the NODE_ENV var.

I am also facing this issue. I was initially getting errors regarding NextRouter not mounted and tried to narrow down the root cause and eventually came across this issue. Both NextRouter not mounted and <Html> should not be imported outside of pages/_document were followed by Error occurred prerendering page...

OS information:

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Fri Jan 27 02:56:13 UTC 2023
Binaries:
  Node: 18.18.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.8.0
Relevant Packages:
  next: 13.5.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

Note: I’ve tried using npm instead of pnpm, but the issue persists. For me, it seems to be happening only in non-Windows environments. (It builds successfully on Windows).

Lastly, it seems that I’m also getting You are using a non-standard "NODE_ENV" value in your environment. (despite setting it to either production or development in my Dockerfile. Is this related?

I can confirm, that after removing ENV NODE_ENV=development from my Dockerfile, the build now succeeds. Can someone from the main next.js team confirm if this is intended? (I would prefer if this were not the case).

Additionally, set NODE_ENV=development in Windows will cause the build to fail as well, so it’s not OS/platform-dependent.

Thanks for the additional reproductions!

Interesting, it seems that Next.js thinks that development is a non-standard NODE_ENV, which is not true according to the docs page:

$ NODE_ENV=development next build
 ⚠ You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env
 ✓ Linting and checking validity of types    
   ▲ Next.js 14.0.2-canary.3

 ✓ Creating an optimized production build    
 ✓ Compiled successfully
 ✓ Collecting page data    
   Generating static pages (0/3)  [=   ]Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
    at useRouter (/workspace/.next/server/chunks/163.js:1:15612)

Created a reproduction of the <Html> should not be imported outside of pages/_document error on Next.js 14.0.2-canary.3:

CodeSandbox: https://codesandbox.io/p/sandbox/fervent-stonebraker-mn8sln?file=%2Fpackage.json%3A8%2C18

Screenshot 2023-11-01 at 12 05 19

Hi, is there any update regarding this?

I was using bun and when I switched back to npm it started working for me. Unfortunate because using bun was a much better developer experience.

I still see this happening on my monorepo with the 14.0.0 release 😦

I’m getting the same issue with next@14.0.1, it works fine when I build locally using my M2 chip Mac, but not when I try to deploy on Vercel…

Also experiencing this issue, temporarly downgrading nextjs to 13.4.12 has resolved it but im sure that’s not the fix we’re looking for…

Getting this error in a newly created create-next-app with the with-supabase template. Using npm and seeing this when running next build

https://github.com/vercel/next.js/tree/canary/examples/with-supabase

Interesting, also seeing these failures on Pages Router when using NODE_ENV=test next dev (“Test default values [from the .env.test file] will be loaded if NODE_ENV is set to test” as per the Next.js environment variables docs) on latest canary next@13.5.7-canary.25

Screenshot 2023-10-25 at 13 48 30

$ NODE_ENV=test yarn dev
$ next dev --port 3020
   ▲ Next.js 13.5.7-canary.25
   - Local:        http://localhost:3020
   - Environments: .env.test

 ✓ Ready in 3.1s
 ✓ Compiled /middleware in 276ms (63 modules)
 ○ Compiling / ...
 ✓ Compiled / in 2s (403 modules)
Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
    at useRouter (/Users/k/p/courses/node_modules/next/dist/client/router.js:146:15)
    at App (webpack-internal:///./pages/_app.tsx:101:77)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
 ⨯ Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
    at useRouter (/Users/k/p/courses/node_modules/next/dist/client/router.js:146:15)
    at App (webpack-internal:///./pages/_app.tsx:101:77)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
    at useRouter (/Users/k/p/courses/node_modules/next/dist/client/router.js:146:15)
    at App (webpack-internal:///./pages/_app.tsx:101:77)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
    at useRouter (/Users/k/p/courses/node_modules/next/dist/client/router.js:146:15)
    at App (webpack-internal:///./pages/_app.tsx:101:77)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
 ○ Compiling /_error ...
 ✓ Compiled /_error in 571ms (405 modules)
Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
 ⨯ Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
 ⨯ Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
 ⚠ Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/messages/fast-refresh-reload
 ⨯ Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
    at useRouter (/Users/k/p/courses/node_modules/next/dist/client/router.js:146:15)
    at App (webpack-internal:///./pages/_app.tsx:101:77)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14) {
  page: '/'
}
 ⨯ Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
    at useRouter (/Users/k/p/courses/node_modules/next/dist/client/router.js:146:15)
    at App (webpack-internal:///./pages/_app.tsx:101:77)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14) {
  page: '/'
}
Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
 ⨯ Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
 ⨯ Error: <Html> should not be imported outside of pages/_document.
Read more: https://nextjs.org/docs/messages/no-document-import-in-page
    at ei (/Users/k/p/courses/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:16:4358)
    at Html (/Users/k/p/courses/node_modules/next/dist/pages/_document.js:691:132)
    at renderWithHooks (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderIndeterminateComponent (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5785:7)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (/Users/k/p/courses/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)

I remember reporting an issue with NODE_ENV=test also causing failures, but not sure if related:

cc @huozhi and @ijjk

nextjs@13.5.4 works fine on my local , yarn build without error, But I have the issue when run yarn build inside docker image with node user from node:18-bookworm-slim,

So I have to change the user of docker image to root