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

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 21.6.0: Mon Apr 24 21:10:53 PDT 2023; root:xnu-8020.240.18.701.5~1/RELEASE_X86_64
    Binaries:
      Node: 20.7.0
      npm: 10.1.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant Packages:
      next: 13.5.2
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.2.2
    Next.js Config:
      output: N/A

Which example does this report relate to?

npm run build

What browser are you using? (if relevant)

chrome

How are you deploying your application? (if relevant)

No response

Describe the Bug

with next 13.5.2 fresh installation then run build i have this error

same with latest canary 13.5.3-canary.1 Screen Shot 2023-09-22 at 11 28 30 AM Screen Shot 2023-09-22 at 11 29 10 AM

after going back to 13.4.19 Screen Shot 2023-09-22 at 11 31 58 AM

Expected Behavior

no errors

To Reproduce

npm run build

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Reactions: 1
  • Comments: 16 (5 by maintainers)

Most upvoted comments

@ahmedyounes seems I was on the right track šŸ˜‰

Closing as this seems to have been figured out. FWIW, setting NODE_ENV explicitly should not be needed in general. Since there was no reproduction attached, itā€™s hard to tell what could have been an issue. Feel free to open a bug report (using the correct issue template).

Hi, I update my reproduction https://github.com/Yovach/nextjs-app-router-document The issue is that Next.js error is unclear.

In my example, it displays :

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

and 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.

But if we donā€™t look at the source code, we donā€™t know that NODE_ENV to development isnā€™t compatible with next build

It is also an issue if you run your Next.js solution in a Yarn workspace requirement. To be able to locate files in projects outside the next project folder you need to have NODE_ENV set to development (which by the way is a valid NODE_ENV value and not ā€œNon-standardā€ as the warning might indicate).

This means that you are not able to build Next projects with yarn build if itā€™s inside a yarn workspace.

This was no issue prior to 13.5.X because then it worked perfectly running yarn build with NODE_ENV set to development so this is a new behavior introduced sometime after 13.4.0

Can confirm. This became issue after upgrading to 13.5.3. I had no issues with building in 13.5.2. Iā€™m using SST to deploy our project to different environments including previews for PRs and deploying them started to fail, because we donā€™t set NODE_ENV to production. It was working with no issues a day or two ago.

To fix this issue, you must be sure that your NODE_ENV is set to development.

You probably mean: To fix this issue, you must be sure that your NODE_ENV is set to production. šŸ¤”

Oops !! Sorry, fixed šŸ˜ƒ

Ok, I just discovered that if I run my build command without env-cmd (I use it to change the env file loaded for build since next build loads automatically .env.production) it works!!!

I also had the message:

āš  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

That may be a starting point to look for this bug?