Description
Bug 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).