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.
npx create-next-app@latest test
- Enter the generated folder
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
About this issue
- Original URL
- State: open
- Created 9 months ago
- Reactions: 15
- Comments: 24 (7 by maintainers)
Commits related to this issue
- fix: set NODE_ENV to production to fix builds See https://github.com/vercel/next.js/issues/56481 — committed to jonahsnider/frc-colors.com by jonahsnider 8 months ago
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. BothNextRouter not mounted
and<Html> should not be imported outside of pages/_document
were followed byError occurred prerendering page...
OS information:
Note: I’ve tried using
npm
instead ofpnpm
, 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 eitherproduction
ordevelopment
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-standardNODE_ENV
, which is not true according to the docs page:Created a reproduction of the
<Html> should not be imported outside of pages/_document
error on Next.js14.0.2-canary.3
:CodeSandbox: https://codesandbox.io/p/sandbox/fervent-stonebraker-mn8sln?file=%2Fpackage.json%3A8%2C18
Hi, is there any update regarding this?
I was using
bun
and when I switched back tonpm
it started working for me. Unfortunate because usingbun
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 thewith-supabase
template. Using npm and seeing this when runningnext 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 ifNODE_ENV
is set totest
” as per the Next.js environment variables docs) on latest canarynext@13.5.7-canary.25
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 runyarn build
inside docker image withnode
user fromnode:18-bookworm-slim
,So I have to change the user of docker image to root
I believe @harrisyn is facing this issue as well (https://github.com/vercel/next.js/issues/54961#issuecomment-1727594268)