Fail next build early when NODE_ENV=development (#101978)
### What?
`next build` now fails early, before compiling, with a clear error when
`NODE_ENV=development` is set and the build isn't opted into development
builds. A common cause is a leftover `NODE_ENV=development` in a shell
profile, Dockerfile or CI config.
```
> "NODE_ENV" is set to "development", but `next build` creates a production build. Remove NODE_ENV from the environment that runs `next build` (e.g. your shell, Dockerfile or CI settings). To create a development build instead, set NODE_ENV=development and enable `experimental.allowDevelopmentBuild`. Read more: https://nextjs.org/docs/messages/non-standard-node-env
```
Before this change, an App Router build compiled and then failed while
prerendering, and the error didn't mention `NODE_ENV`:
```
Error occurred prerendering page "/_global-error".
TypeError: Cannot read properties of null (reading 'useContext')
```
These cases are unchanged:
- `NODE_ENV` unset (the CLI defaults it to `production`), `production`
and `test`.
- Other non-standard values such as `staging`: they still only get the
existing generic warning, and the build continues.
- `NODE_ENV=development` with `experimental.allowDevelopmentBuild`, or
`next build --debug-prerender` (which enables it). Both produce a
consistent development build that can prerender.
- `next dev` and `next start`.
> [!WARNING]
> **Breaking change:** `next build` with `NODE_ENV=development` and no
opt-in now fails, including builds that used to succeed (e.g. some Pages
Router builds). To fix it, remove `NODE_ENV` from the build environment,
or opt into a development build explicitly. Other `NODE_ENV` values are
not affected. This should go in the changelog.
### Why?
Unless `allowDevelopmentBuild` is enabled, `next build` inlines
`process.env.NODE_ENV` as `'production'` in the bundles. The build
process and its prerender workers kept the caller's `development`,
though. Modules that pick an implementation at runtime, such as the
app-page runtime selector `module.compiled.js` (which checks `===
'development'`), then loaded development runtimes next to production
chunks. That produced two copies of React and a null `useContext` during
prerendering. The only hint was the generic non-standard `NODE_ENV`
warning at startup, which is why this report keeps coming back.
Overriding `NODE_ENV` to `production` (an earlier version of this PR)
can't be made consistent. The main process has already evaluated
`next.config` with the caller's value, and workers (e.g. the webpack
build worker) evaluate it again, so a config that branches on `NODE_ENV`
(e.g. `basePath`) resolves two ways in one build. Failing early keeps a
single `next.config` evaluation and builds nothing in a mismatched
environment.
Other non-standard values are left as they are. They weren't shown to
crash (e.g. `abc` built fine), and failing them would break existing
pipelines.
Related: #87719, #97046, #95119. Some reports in #87719 have other
causes (e.g. `pnpx` running a different Next.js version), so this PR
doesn't close it.
### How?
Right after `next.config` is loaded (so `allowDevelopmentBuild` is
known), `build()` throws a `NON_STANDARD_NODE_ENV_BUILD` error when
`NODE_ENV === 'development'` and the opt-in is off. That's before the
page scan, compilation, workers and prerendering. `cli/next-build.ts`
prints it as a single `> …` line without a stack, like other expected
build errors. `process.env.NODE_ENV` is never modified.
Docs: `errors/non-standard-node-env.mdx` gets a new "`NODE_ENV` during
`next build`" section. `docs/01-app/02-guides/environment-variables.mdx`
gets a new "Good to know" bullet (shared with the Pages Router guide).
### Verification
- New `test/production/app-dir/build-node-env-development` (an App
Router app without a custom global-error):
- `NODE_ENV=development` fails with a non-zero exit. The error names the
value, and there's no `useContext` or prerender error, no `Build error
occurred` stack, and no prerendered output.
- `NODE_ENV=abc` builds and prerenders, with only the generic warning.
- `--debug-prerender` and `NODE_ENV=test` builds succeed.
- Passes with `pnpm test-start-turbo` and `pnpm test-start-webpack`,
with Cache Components on and off.
- Existing suites, to confirm nothing else changed:
- `pnpm test-start-turbo` / `pnpm test-start-webpack
test/production/allow-development-build/` (config-level opt-in)
- `pnpm test-start-turbo` / `pnpm test-start-webpack
test/e2e/non-standard-node-env-warning/` (its `next build` +
`NODE_ENV=development` case now also expects the error)
- `pnpm test-dev-turbo test/e2e/non-standard-node-env-warning/`
- Manual:
- `NODE_ENV=development next build` prints the generic warning and the
error above, and exits 1.
- `NODE_ENV=staging next build` exits 0 and prerenders.
- With `NODE_ENV=development` only in `.env` (and unset in the shell),
`next build` exits 0: the CLI sets `NODE_ENV` before `.env*` files are
loaded, so they can't trigger this error. The error and docs therefore
point at the build environment, not at `.env*` files.
- Not run:
- The full test suite and deploy tests (the change only affects local
`next build` env validation).
- Historical baselines or a bisect. The same symptom is reported back to
16.0.x, so the change that introduced it isn't established.
<!-- NEXT_JS_LLM -->
<!-- fleet 045bee0e-99ad-4780-8c08-dbc3f8ec9f94 -->
---------
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Luke Sandberg <210140+lukesandberg@users.noreply.github.com>