{"id":15718,"library":"next-runtime-env","title":"Next.js Runtime Environment Configuration","description":"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).","status":"active","version":"3.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/expatfile/next-runtime-env","tags":["javascript","nextjs","runtime","env","runtime env","next-runtime-env","typescript"],"install":[{"cmd":"npm install next-runtime-env","lang":"bash","label":"npm"},{"cmd":"yarn add next-runtime-env","lang":"bash","label":"yarn"},{"cmd":"pnpm add next-runtime-env","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for Next.js framework integration.","package":"next","optional":false},{"reason":"Peer dependency for React component usage.","package":"react","optional":false}],"imports":[{"note":"This is a named export. It must be placed in the `<head>` of your `app/layout.tsx` (App Router) or `pages/_document.tsx` (Pages Router) to expose environment variables client-side.","wrong":"import PublicEnvScript from 'next-runtime-env';","symbol":"PublicEnvScript","correct":"import { PublicEnvScript } from 'next-runtime-env';"},{"note":"The `env` function is a named export used in client components to access runtime-injected variables. Do not use `process.env` in client components for runtime variables as it will only reflect build-time values.","wrong":"import env from 'next-runtime-env';\nprocess.env.NEXT_PUBLIC_VAR;","symbol":"env","correct":"import { env } from 'next-runtime-env';"},{"note":"This named export is used programmatically to expose additional, non-`NEXT_PUBLIC_` prefixed environment variables to the client-side runtime, requiring specific configuration.","wrong":"import makeEnvPublic from 'next-runtime-env';","symbol":"makeEnvPublic","correct":"import { makeEnvPublic } from 'next-runtime-env';"}],"quickstart":{"code":"import { PublicEnvScript } from 'next-runtime-env';\nimport { env } from 'next-runtime-env';\nimport React, { useEffect, useState } from 'react';\n\n// next.config.mjs (Optional, but recommended for Next.js 14 / v3.x)\n/** @type {import('next').NextConfig} */\nconst nextConfig = {\n  experimental: {\n    // This ensures next-runtime-env functions correctly with server components\n    serverComponentsExternalPackages: ['next-runtime-env'],\n  },\n};\nexport default nextConfig;\n\n// app/layout.tsx\n// Place PublicEnvScript in the <head> of your root layout\nexport default function RootLayout({\n  children,\n}: { children: React.ReactNode }) {\n  return (\n    <html lang=\"en\">\n      <head>\n        <PublicEnvScript />\n      </head>\n      <body>\n        <h1>My Next.js Runtime Environment App</h1>\n        {children}\n      </body>\n    </html>\n  );\n}\n\n// app/client-page.tsx (Example of consuming variables client-side)\n'use client'; // Mark as a client component\n\nexport default function SomePage() {\n  const [fooEnv, setFooEnv] = useState<string | undefined>();\n  const [barEnv, setBarEnv] = useState<string | undefined>();\n\n  useEffect(() => {\n    // Access variables using the 'env' function\n    setFooEnv(env('NEXT_PUBLIC_FOO_RUNTIME'));\n    setBarEnv(env('NEXT_PUBLIC_BAR_RUNTIME'));\n  }, []);\n\n  return (\n    <main style={{ padding: '20px', border: '1px solid #eee', margin: '20px' }}>\n      <h2>Client-Side Runtime Variables</h2>\n      <p>NEXT_PUBLIC_FOO_RUNTIME: {fooEnv ?? 'Not set'}</p>\n      <p>NEXT_PUBLIC_BAR_RUNTIME: {barEnv ?? 'Not set'}</p>\n      <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>\n    </main>\n  );\n}\n\n// Example .env.local file (for local development)\n// NEXT_PUBLIC_FOO_RUNTIME=hello-from-runtime-dev\n// NEXT_PUBLIC_BAR_RUNTIME=another-runtime-var-dev","lang":"typescript","description":"This quickstart demonstrates how to configure `next-runtime-env` for a Next.js App Router project (v3.x), placing `PublicEnvScript` in `app/layout.tsx` to expose environment variables and then accessing them in a client component using the `env` function."},"warnings":[{"fix":"Consult the `next-runtime-env` documentation for the correct version matching your Next.js setup: v1.x for Next.js 12/13 Pages Router, v2.x for Next.js 13 App Router, and v3.x for Next.js 14 App Router with improved caching.","message":"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.","severity":"breaking","affected_versions":">=1.0"},{"fix":"Always use the `env('YOUR_VAR_NAME')` function provided by `next-runtime-env` within client components to correctly access environment variables exposed at runtime. `process.env` is only reliable for server-side or build-time variables.","message":"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`.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Add `next-runtime-env` to the `experimental.serverComponentsExternalPackages` array in your `next.config.js` to ensure proper functionality and avoid potential issues with server-side rendering and hydration.","message":"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`.","severity":"gotcha","affected_versions":">=3.2.2"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"For runtime-injected environment variables in client-side code, use `import { env } from 'next-runtime-env';` and then `env('YOUR_VAR_NAME')`.","cause":"Attempting to access `process.env.VAR_NAME` directly in a client-side component or browser environment where `process` is not available.","error":"ReferenceError: process is not defined"},{"fix":"Ensure `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`.","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).","error":"Environment variables appear as `undefined` or empty strings on the client."},{"fix":"Check 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`.","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).","error":"Error: \"next-runtime-env\" is not compatible with Next.js X. Please use version Y. (or similar message indicating version mismatch)"}],"ecosystem":"npm"}