Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Next.js is correctly respecting NODE_ENV #19046

Open
2 of 5 tasks
Timer opened this issue Nov 11, 2020 · 14 comments
Open
2 of 5 tasks

Ensure Next.js is correctly respecting NODE_ENV #19046

Timer opened this issue Nov 11, 2020 · 14 comments
Assignees
Labels
bug Issue was opened via the bug report template. Webpack Related to Webpack with Next.js.

Comments

@Timer
Copy link
Member

Timer commented Nov 11, 2020

Next.js should unconditionally use NODE_ENV=development and NODE_ENV=production for next dev and next build respectively in the following areas:

The only way to read a "misconfigured" NODE_ENV (e.g. NODE_ENV=staging) should be in next.config.js. This is only allowed for backwards compatibility.

I believe we also have to allow it to override what Babel "env" is loaded. I don't feel strongly about this.

Next 11 should disallow all non-standard values for this and immediately exit. This behavior should be implemented by whatever PR closes this behind a future flag.


We also need to fix this message:

warn  - 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://err.sh/next.js/non-standard-node-env
@Timer Timer added this to the iteration 13 milestone Nov 11, 2020
@Timer Timer added point: 5 and removed point: 3 labels Nov 11, 2020
@timneutkens timneutkens modified the milestones: iteration 13, iteration 14 Dec 1, 2020
@Timer
Copy link
Member Author

Timer commented Dec 31, 2020

Related: #17032 #19001

@Timer Timer self-assigned this Dec 31, 2020
Timer added a commit to Timer/next.js that referenced this issue Jan 1, 2021
This PR fixes a bug where `next/babel` would accidentally enable development transforms for a production build (`next build`).

---

Fixes vercel#18929
Fixes vercel#19001
x-ref vercel#19046
Timer added a commit to Timer/next.js that referenced this issue Jan 1, 2021
This PR fixes a bug where `next/babel` would accidentally enable development transforms for a production build (`next build`).

---

Fixes vercel#18929
Fixes vercel#19001
x-ref vercel#19046
Timer added a commit to Timer/next.js that referenced this issue Jan 1, 2021
This PR fixes a bug where `next/babel` would accidentally enable development transforms for a production build (`next build`).

---

Fixes vercel#18929
Fixes vercel#19001
x-ref vercel#19046
kodiakhq bot pushed a commit that referenced this issue Jan 2, 2021
This PR fixes a bug where `next/babel` would accidentally enable development transforms for a production build (`next build`).

This is tested by the two updated unit tests (which removed a workaround for this bug, and one now properly enables dev transforms).

---

Fixes #18929
Fixes #19001
x-ref #19046
x-ref #17032
@Timer Timer modified the milestones: iteration 17, 10.x.x Mar 15, 2021
@flybayer
Copy link
Contributor

flybayer commented Jun 3, 2021

[ ] .env.* loading

I believe we should add an alternate way to specify which env files should be loaded, like this:

next dev -e [test|development|production]

@curiousercreative
Copy link

curiousercreative commented Jun 16, 2021

@flybayer that would solve my exact frustration. Our application doesn't care about the NODE_ENV, but we need our staging deploys to use non-production constants from a non-production .env file. I'd be careful not to restrict the values that we support for mapping to an env.* file. An obvious omission from your enumerated list is a staging environment, but I'd suggest we do not restrict these values.

curiousercreative added a commit to curiousercreative/next.js that referenced this issue Jun 16, 2021
…d be loaded

attempts to partially solve vercel#19046 (.env file loading)
@curiousercreative
Copy link

curiousercreative commented Jun 16, 2021

Here's a quick and dirty that should allow for DOT_ENV=staging next build to load .env.staging rather than .env.production while preserving prior behavior when DOT_ENV has not been supplied. #26209

@curiousercreative
Copy link

curiousercreative commented Jun 16, 2021

@Timer @flybayer @timneutkens thoughts on allowing user to determine which .env override file is loaded? Thoughts on env variable name DOT_ENV as the means for accomplishing this? Should we go even further and allow for a list of .env files to be selected?

@flybayer
Copy link
Contributor

@curiousercreative I've recently opened an extensive RFC here: #25764

@Raynos
Copy link

Raynos commented Jul 9, 2023

I just tried NODE_ENV=development next build to use the non minified version of react and it refuses to use the non minified version of react.

This magic build into nextjs is incredibly frustration and a terrible user experience.

@frixaco

This comment has been minimized.

@bailout00
Copy link

Looks like this is still open, and still an issue, causes Error: should not be imported outside of pages/_document.
if NODE_ENV=development when running next build

@RobertMrowiec

This comment was marked as spam.

@hichemfantar
Copy link
Contributor

Looks like this is still open, and still an issue, causes Error: should not be imported outside of pages/_document. if NODE_ENV=development when running next build

does it work when you set it to production?

@christopher-theagen
Copy link

#52158

@samcx samcx added bug Issue was opened via the bug report template. and removed kind: bug labels Apr 30, 2024
@timeverts
Copy link

Looks like this is still open, and still an issue, causes Error: should not be imported outside of pages/_document. if NODE_ENV=development when running next build

I also get this error, only when setting NODE_ENV=development when running next build.

@ChenAlmost
Copy link

ChenAlmost commented Aug 1, 2024

I solve this problem by add a APP_ENV when I run next build and use dotenv read the .env.* according to the value of APP_ENV in next.config file, overwrite the process.env.*

Here's a quick and dirty that should allow for DOT_ENV=staging next build to load .env.staging rather than .env.production while preserving prior behavior when DOT_ENV has not been supplied. #26209

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Webpack Related to Webpack with Next.js.
Projects
None yet
Development

No branches or pull requests