@devlu/nextenv
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Next.js Runtime Environment Variables

Getting Started

Install

npm i @devlu/nextenv

Add the following to your app/layout.tsx file:

// app/layout.tsx
import { EnvProvider } from '@devlu/nextenv'

export const dynamic = 'force-dynamic' // <-- this is required

export default function Layout({ children }) {
  return (
    <html lang="en">
      <body>
        <EnvProvider>
          {children}
        </EnvProvider>
      </body>
    </html>
  )
}

Usage

For Server-Side

import { useEnv } from '@devlu/nextenv'

export default function Home() {
  const env = useEnv()
  return (
    <div>
      <h1>{env.MY_ENV_VAR}</h1>
    </div>
  )
}

For Client-Side

Only variables that start with ENV_PUBLIC_ are available on the client side.

'use client'

import { useClientEnv } from '@devlu/nextenv'

export default function Home() {
  const env = useClientEnv()
  return (
    <div>
      <h1>{env.ENV_PUBLIC_MY_ENV_VAR}</h1>
    </div>
  )
}

Dependencies (0)

    Dev Dependencies (9)

    Package Sidebar

    Install

    npm i @devlu/nextenv

    Weekly Downloads

    0

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    6.3 kB

    Total Files

    15

    Last publish

    Collaborators

    • devlu