The test NODE_ENV isn't being set properly
See original GitHub issueBug report
Describe the bug
Currently, Next.js only supports three different values for process.env.NODE_ENV
(as documented here): development
, production
, and test
.
When running NODE_ENV='test' next build
, Next.js loads the right .env.test
files but fails to properly replace process.env.NODE_ENV
references with test
(instead of production
).
Previous issues (e.g. #9123, #12772) have requested support for more environment variables. This is not what this issue is about. This is an issue with behaviour that Next.js has already documented as supported (e.g. @Timer says that test
is supported in this comment).
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Clone this repository:
$ git clone https://github.com/nicholaschiang/env-demo && cd env-demo
- Install dependencies:
$ yarn install
- Build the app using the
test
environment:
$ yarn test:build
- Start the app using the
test
environment:
$ yarn test:start
- Visit the API route at http://localhost:3000/api/env and notice that the JSON response contains
production
instead oftest
. This is because Next.js hardcodes (i.e. replaces) theprocess.env.NODE_ENV
variable toproduction
when runningnext build
. This is not ideal when you want to use theNODE_ENV
variable to trigger certain behavior during tests (e.g. accessing different Algolia search indexes).
Expected behavior
When running next build
with the NODE_ENV set to test
, Next.js should properly replace process.env.NODE_ENV
references with test
instead of production
.
System information
- OS: PopOS 20.x
- Version of Next.js: 9.5.3
- Version of Node.js: 12.18.3
Additional context
This can be used to switch between outside resources (e.g. using a different set of Algolia indexes than the ones used in production) during tests. While this might not be ideal, it’s required when running integration tests for me (because I can’t easily stub out Algolia’s search capabilities).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:12 (8 by maintainers)
Top GitHub Comments
@Timer you know that’s not what I’m saying, right?
I want Next.js to load the
.env.test
files, not the.env.production
files. I just want it to maintain consistency and also replaceprocess.env.NODE_ENV
withtest
instead ofproduction
.Using the development server (i.e.
NODE_ENV='test' next dev
) during tests is problematic because:next build
andnext start
(e.g. statically optimized pages aren’t statically optimized and take a long time to load).Also, doesn’t Next.js enforce
NODE_ENV='development'
when runningnext dev
too (just like how it enforcesNODE_ENV='production'
when runningnext start
ornext build
)? If so, when are we supposed to useNODE_ENV='test'
?@timneutkens that makes sense. Thus, Next.js enforces
production
duringnext build
to avoid any of those performance de-optimizations. But then why does Next.js even supportNODE_ENV='test'
? What is it’s intended use case?Right now, I’ve got my Cypress integration tests working by:
NODE_ENV='test'
duringnext build
andnext start
. This enables me to load.env.test
and.env.test.local
files which include different API keys (e.g. for Firebase Authentication which doesn’t have a local emulator) thanproduction
anddevelopment
(e.g. so that development changes don’t effect tests).APP_ENV='test'
variable (within the.env.test
file) to reference within back-end code and switch between different Algolia indexes (i.e. usingtest-users
instead ofproduction-users
ordevelopment-users
).By “fixing” this issue, would Next.js no longer load the
.env.test
files duringnext build
andnext start
? Because that would break my current use case.