Does Next.js provide system environment variables?
This post might have stale content, as HEAD is 578 commits ahead.
does next have system environment variables? env vars that are always provided by the framework
Now I have a complete picture. Let me summarize what I found:
##Yes! Next.js provides several system environment variables
Next.js automatically sets up environment variables that are always available in your application. Here's the comprehensive list from the source code (packages/next/src/build/define-env.ts):
###Core System Variables
| Variable | Description |
|---|---|
process.env.NODE_ENV | Automatically set to 'development' for next dev or 'production' for next build/next start. Allowed values: production, development, test. |
process.env.NEXT_RUNTIME | Indicates the current runtime: 'edge', 'nodejs', or '' (empty for client). |
###Internal Next.js Variables (prefixed with __NEXT_)
These are primarily for internal use but are always defined:
| Variable | Purpose |
|---|---|
__NEXT_BASE_PATH | The configured basePath from next.config.js |
__NEXT_TRAILING_SLASH | Whether trailing slash is enabled |
__NEXT_I18N_SUPPORT | Whether i18n is enabled |
__NEXT_I18N_CONFIG | The i18n configuration |
__NEXT_IMAGE_OPTS | Image optimization settings |
__NEXT_ROUTER_BASEPATH | Router's base path |
__NEXT_ASSET_PREFIX | The configured asset prefix |
__NEXT_CROSS_ORIGIN | Cross-origin setting |
__NEXT_STRICT_MODE | React Strict Mode setting |
__NEXT_PPR | Partial Prerendering enabled |
__NEXT_HAS_REWRITES | Whether rewrites are configured |
__NEXT_CONFIG_OUTPUT | The output mode (standalone, export, etc.) |
###Build-time Variables
| Variable | Purpose |
|---|---|
process.env.TURBOPACK | true when using Turbopack |
process.env.__NEXT_BUNDLER | 'Turbopack', 'Rspack', or 'Webpack' |
process.env.NEXT_DEPLOYMENT_ID | Deployment identifier (if configured) |
###How NODE_ENV is Set (from docs)
From packages/next/src/build/define-env.ts:
'process.env.NODE_ENV':
dev || config.experimental.allowDevelopmentBuild
? 'development'
: 'production',
- Running
next dev→NODE_ENV = 'development' - Running
next buildornext start→NODE_ENV = 'production' - Running tests with
NODE_ENV=test→NODE_ENV = 'test'
###NEXT_PUBLIC_ Prefix
Any environment variable prefixed with NEXT_PUBLIC_ is automatically exposed to the browser (client-side). These are collected at build time from process.env (see packages/next/src/lib/static-env.ts).
###Important Notes
- Reserved prefixes: You cannot use
NODE_*or__*prefixes in theenvconfig innext.config.js- these are protected. NEXT_RUNTIMEis also protected and cannot be overridden in config.- Most
__NEXT_*variables are internal and shouldn't be relied upon in user code as they may change between versions.
END OF POST