npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

next-env-safe

v1.0.2

Published

Type-safe, Next.js-ready environment variable manager (server/client separation, Zod validation, build-time fail).

Readme

next-env-safe

A lightweight, type-safe environment manager for Next.js, built on top of Zod.
Designed to keep server secrets secure while providing fully-typed client and server environment variables with excellent Developer Experience.



🚀 Features

  • ✅ Strong TypeScript inference using Zod
  • ✅ Prevents accidental server → client secret leakage
  • ✅ Throws build-time errors for missing/invalid env variables
  • ✅ Works with Next.js App Router and Pages Router
  • ✅ Simple, minimal, predictable API
  • ✅ No runtime bloat → extremely small & tree-shakeable

📦 Installation

npm install next-env-safe zod

or

pnpm add next-env-safe zod

🧩 Quick Start

Create a file called:

env.ts

Then define your environment schema:

import { z } from "zod";
import { createEnv } from "next-env-safe";

export const env = createEnv({
  server: {
    DATABASE_URL: z.string().url(),
    SECRET_KEY: z.string().min(32),
  },
  client: {
    NEXT_PUBLIC_API_URL: z.string().url(),
  },
  runtimeEnv: process.env, // usually just process.env
});

✅ Usage

✅ Server Components, Server Actions, or API Routes

import { env } from "@/env";

console.log(env.DATABASE_URL);

✅ Client Components

import { env } from "@/env";

export default function Page() {
  return <div>{env.client.NEXT_PUBLIC_API_URL}</div>;
}

🔒 Security

If you attempt to access a server-only environment variable on the client,
you will get a clear runtime error:

❌ Attempted to access server environment key "SECRET_KEY" from the client.

This ensures sensitive secrets never leak into the browser bundle.


⚙️ API

createEnv({
  server: {
    // Zod schemas for server-only variables
  },
  client: {
    // Zod schemas for client-side variables starting with NEXT_PUBLIC_
  },
  runtimeEnv: process.env, // or custom env object
  clientPrefix: string, // default: "NEXT_PUBLIC_"
  verbose: boolean, // log warnings
});

Return Value:

  • Server environment values (only available on server)
  • Client environment values (available everywhere)
  • env.client — a safe subset for client components

✅ Rules

Server Schema

  • Server env keys must NOT start with the client prefix
    (default: NEXT_PUBLIC_)

Client Schema

  • Client env keys must start with NEXT_PUBLIC_
    (or your custom prefix)

Validation

  • On the server → both server and client schemas are validated
  • On the client → only the client schema is used
  • Server values are completely removed from browser bundles

🧠 Type Inference Example

const env = createEnv({
  server: { FOO: z.number().int() },
  client: { NEXT_PUBLIC_BAR: z.string() },
  runtimeEnv: process.env,
});

// env.FOO → number
// env.client.NEXT_PUBLIC_BAR → string

❓ FAQ

✅ Can I change the client prefix?

Yes:

createEnv({
  clientPrefix: "PUBLIC_",
  ...
})

All client-visible variables must then start with PUBLIC_.


✅ Will server secrets ever appear in the browser?

❌ No.
Server variables are never included in the client-side bundle.
Accessing them in client code throws an error.


✅ Do I need separate env files for client and server?

No.
A single unified env.ts works everywhere.


📄 License

MIT © Ali