MCPcopy Index your code
hub / github.com/expatfile/next-runtime-env

github.com/expatfile/next-runtime-env @v3.3.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.3.0 ↗ · + Follow
34 symbols 95 edges 46 files 0 documented · 0% 8 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

GitHub branch checks state [codecov]cov-url [Known Vulnerabilities]snyk-url

🌐 Next.js Runtime Environment Configuration

Effortlessly populate your environment at runtime, not just at build time, with next-runtime-env.

🌟 Highlights: - Isomorphic Design: Works seamlessly on both server and browser, and even in middleware. - Next.js 13 & 14 Ready: Fully compatible with the latest Next.js features. - .env Friendly: Use .env files during development, just like standard Next.js.

🤔 Why next-runtime-env?

In the modern software development landscape, the "Build once, deploy many" philosophy is key. This principle, essential for easy deployment and testability, is a cornerstone of continuous delivery and is embraced by the twelve-factor methodology. However, front-end development, particularly with Next.js, often lacks support for this - requiring separate builds for different environments. next-runtime-env is our solution to bridge this gap in Next.js.

📦 Introducing next-runtime-env

next-runtime-env dynamically injects environment variables into your Next.js application at runtime. This approach adheres to the "build once, deploy many" principle, allowing the same build to be used across various environments without rebuilds.

🤝 Compatibility Notes

🔖 Version Guide

  • 1.x: Next.js 12/13 Page Router
  • 2.x: Next.js 13 App Router
  • 3.x: Next.js 14 with advanced caching

🚀 Getting Started

In your app/layout.tsx, add:

// app/layout.tsx
import { PublicEnvScript } from 'next-runtime-env';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <head>
        <PublicEnvScript />
      </head>
      <body>
        {children}
      </body>
    </html>
  );
}

The PublicEnvScript component automatically exposes all environment variables prefixed with NEXT_PUBLIC_ to the browser. For custom variable exposure, refer to EXPOSING_CUSTOM_ENV.md.

🧑‍💻 Usage

Access your environment variables easily:

// app/client-page.tsx
'use client';
import { env } from 'next-runtime-env';

export default function SomePage() {
  const NEXT_PUBLIC_FOO = env('NEXT_PUBLIC_FOO');
  return <main>NEXT_PUBLIC_FOO: {NEXT_PUBLIC_FOO}</main>;
}

🛠 Utilities

Need to expose non-prefixed environment variables to the browser? Check out MAKING_ENV_PUBLIC.md.

👷 Maintenance

next-runtime-env is proudly maintained by Expatfile.tax, the leading US expat tax e-filing software.

📚 Acknowledgments

Kudos to the react-env project for the inspiration, and a shoutout to @andonirdgz for the innovative context provider idea!


Extension points exported contracts — how you extend this code

Window (Interface)
(no doc)
src/index.ts

Core symbols most depended-on inside this repo

formatter
called by 25
src/lib/picocolors.ts
makeEnvPublic
called by 9
src/utils/make-env-public.ts
env
called by 9
src/script/env.ts
getPublicEnv
called by 7
src/helpers/get-public-env.ts
useEnvContext
called by 7
src/provider/use-env-context.ts
prefixedLog
called by 4
src/helpers/log.ts
warn
called by 3
src/helpers/log.ts
event
called by 3
src/helpers/log.ts

Shape

Function 33
Interface 1

Languages

TypeScript100%

Modules by API surface

src/helpers/log.ts5 symbols
src/utils/make-env-public.ts2 symbols
src/lib/picocolors.ts2 symbols
src/script/public-env-script.tsx1 symbols
src/script/env.ts1 symbols
src/script/env-script.tsx1 symbols
src/provider/use-env-context.ts1 symbols
src/provider/use-env-context.spec.tsx1 symbols
src/provider/public-env-provider.tsx1 symbols
src/provider/public-env-provider.spec.tsx1 symbols
src/provider/env-provider.tsx1 symbols
src/provider/env-provider.spec.tsx1 symbols

For agents

$ claude mcp add next-runtime-env \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact