Next.js Runtime Environment Configuration
next-runtime-env is a crucial utility for Next.js applications, enabling the dynamic injection of environment variables at runtime rather than solely at build time. This capability is fundamental for adhering to the "build once, deploy many" philosophy, a cornerstone of continuous delivery and the twelve-factor methodology, which Next.js often lacks native support for in frontend contexts. The library allows developers to use the same build artifact across various environments, such as development, staging, and production, without the need for environment-specific rebuilds. The current stable version is `3.3.0`, with recent alpha and stable releases indicating active maintenance. It offers isomorphic design, ensuring seamless operation across server, browser, and middleware environments. Key differentiators include full compatibility with Next.js 13 and 14, and native support for `.env` files during development. The package maintains distinct major versions (1.x, 2.x, 3.x) to align with specific Next.js router types and major versions, providing tailored solutions for the Pages Router (1.x), Next.js 13 App Router (2.x), and Next.js 14 with enhanced caching (3.x).
Common errors
-
ReferenceError: process is not defined
cause Attempting to access `process.env.VAR_NAME` directly in a client-side component or browser environment where `process` is not available.fixFor runtime-injected environment variables in client-side code, use `import { env } from 'next-runtime-env';` and then `env('YOUR_VAR_NAME')`. -
Environment variables appear as `undefined` or empty strings on the client.
cause `PublicEnvScript` is not correctly rendered in the `<head>` of your root layout (`app/layout.tsx` for App Router or `pages/_document.tsx` for Pages Router), or variables are not prefixed with `NEXT_PUBLIC_` (or explicitly exposed).fixEnsure `PublicEnvScript` is present and correctly placed in the `<head>`. Verify that public variables are prefixed with `NEXT_PUBLIC_`. For non-public variables, follow the documentation on using `makeEnvPublic`. -
Error: "next-runtime-env" is not compatible with Next.js X. Please use version Y. (or similar message indicating version mismatch)
cause The installed version of `next-runtime-env` does not match the major version of your Next.js project or its configured router type (Pages Router vs. App Router).fixCheck the `next-runtime-env` compatibility guide in its README. For Next.js 12/13 Pages Router, use `next-runtime-env@1.x`. For Next.js 13 App Router, use `next-runtime-env@2.x`. For Next.js 14 App Router, use `next-runtime-env@3.x`.
Warnings
- breaking next-runtime-env has distinct major versions that are compatible with specific Next.js major versions and router types. Using an incompatible version will lead to unexpected behavior or breakage.
- gotcha When using `next-runtime-env`, developers might mistakenly attempt to access runtime-injected environment variables via `process.env` in client-side code, which will only yield build-time values or `undefined`.
- gotcha For `next-runtime-env` v3.x and Next.js 14, specifically when using server components or a 'context approach', you might need to explicitly configure `next-runtime-env` in `serverComponentsExternalPackages` within `next.config.js`.
Install
-
npm install next-runtime-env -
yarn add next-runtime-env -
pnpm add next-runtime-env
Imports
- PublicEnvScript
import PublicEnvScript from 'next-runtime-env';
import { PublicEnvScript } from 'next-runtime-env'; - env
import env from 'next-runtime-env'; process.env.NEXT_PUBLIC_VAR;
import { env } from 'next-runtime-env'; - makeEnvPublic
import makeEnvPublic from 'next-runtime-env';
import { makeEnvPublic } from 'next-runtime-env';
Quickstart
import { PublicEnvScript } from 'next-runtime-env';
import { env } from 'next-runtime-env';
import React, { useEffect, useState } from 'react';
// next.config.mjs (Optional, but recommended for Next.js 14 / v3.x)
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
// This ensures next-runtime-env functions correctly with server components
serverComponentsExternalPackages: ['next-runtime-env'],
},
};
export default nextConfig;
// app/layout.tsx
// Place PublicEnvScript in the <head> of your root layout
export default function RootLayout({
children,
}: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<PublicEnvScript />
</head>
<body>
<h1>My Next.js Runtime Environment App</h1>
{children}
</body>
</html>
);
}
// app/client-page.tsx (Example of consuming variables client-side)
'use client'; // Mark as a client component
export default function SomePage() {
const [fooEnv, setFooEnv] = useState<string | undefined>();
const [barEnv, setBarEnv] = useState<string | undefined>();
useEffect(() => {
// Access variables using the 'env' function
setFooEnv(env('NEXT_PUBLIC_FOO_RUNTIME'));
setBarEnv(env('NEXT_PUBLIC_BAR_RUNTIME'));
}, []);
return (
<main style={{ padding: '20px', border: '1px solid #eee', margin: '20px' }}>
<h2>Client-Side Runtime Variables</h2>
<p>NEXT_PUBLIC_FOO_RUNTIME: {fooEnv ?? 'Not set'}</p>
<p>NEXT_PUBLIC_BAR_RUNTIME: {barEnv ?? 'Not set'}</p>
<p><em>To see these, ensure NEXT_PUBLIC_FOO_RUNTIME and NEXT_PUBLIC_BAR_RUNTIME are in your .env.local or deployment environment.</em></p>
</main>
);
}
// Example .env.local file (for local development)
// NEXT_PUBLIC_FOO_RUNTIME=hello-from-runtime-dev
// NEXT_PUBLIC_BAR_RUNTIME=another-runtime-var-dev