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

@matthew-hre/env

v0.3.1

Published

Type safe environment variable validation for NextJS projects

Readme

@matthew-hre/env

Type safe environment variable validation for NextJS projects with support for client/server separation.

This package is mostly for my own projects, and I wouldn't recommend using it yourself in its current state. It may be improved in the future. May.

Installation

npm install @matthew-hre/env

Usage

Client/Server Schema (Recommended)

For NextJS projects, you can separate client and server environment variables for better security and type safety:

// lib/env.ts
import { loadEnv } from "@matthew-hre/env";
import { z } from "zod";

const schema = {
  server: z.object({
    NODE_ENV: z.enum(["development", "production"]),
    DATABASE_URL: z.string().url(),
    SECRET_KEY: z.string(),
  }),
  client: z.object({
    NEXT_PUBLIC_API_URL: z.string().url(),
    NEXT_PUBLIC_APP_NAME: z.string(),
  }),
};

// optionally, export the schemas for use elsewhere
export type ServerEnvSchema = z.infer<typeof schema.server>;
export type ClientEnvSchema = z.infer<typeof schema.client>;

export const { serverEnv, clientEnv } = loadEnv(schema);

Legacy Single Schema

For simpler projects or backward compatibility, you can still use the original single schema format:

import { loadEnv } from "@matthew-hre/env";
// lib/env.ts
import { z } from "zod";

const schema = z.object({
  NODE_ENV: z.string(),
  NEXT_PUBLIC_API_URL: z.url(),
});

// optionally, export the schema for use elsewhere
export type EnvSchema = z.infer<typeof schema>;

export const env = loadEnv(schema);

next.config.js

Import the env file in your next.config.js. This is enough to ensure the environment variables are validated at build time.

// next.config.ts
import type { NextConfig } from "next";

import "src/lib/env";

const nextConfig: NextConfig = {
  // ...
};

export default nextConfig;

Examples

Using Client/Server Environment Variables

// server-side code (api routes, etc.)
import { clientEnv, serverEnv } from "src/lib/env";

export async function GET() {
  // can access both server and client variables
  const dbUrl = serverEnv.DATABASE_URL;
  const apiUrl = clientEnv.NEXT_PUBLIC_API_URL;

  // full type safety and autocompletion
  return fetch(`${apiUrl}/data`, {
    headers: { authorization: serverEnv.SECRET_KEY }
  });
}
// client-side code (components, hooks, etc.)
"use client";

import { clientEnv } from "src/lib/env";

export function ApiClient() {
  // can access client variables
  const apiUrl = clientEnv.NEXT_PUBLIC_API_URL;

  return (
    <span>
      API URL:
      {apiUrl}
    </span>
  );
}

Environment Variable Validation

const schema = {
  server: z.object({
    NODE_ENV: z.enum(["development", "production"]),
    DATABASE_URL: z.url(),
  }),
  client: z.object({
    NEXT_PUBLIC_API_URL: z.url(),
  }),
};

Configuration

The loadEnv function accepts optional parameters:

  • env: An object representing the environment variables to validate. Defaults to process.env.
  • options: An object containing options to customize the behavior:
    • exitOnError: If set to true, the process will exit with a non-zero status code if the environment variables are invalid. Defaults to true.
const customEnv = { NODE_ENV: "test", NEXT_PUBLIC_API_URL: "http://localhost:3000" };
const { serverEnv, clientEnv } = loadEnv(schema, customEnv);

const { serverEnv, clientEnv } = loadEnv(schema, process.env, { exitOnError: false });

License

Matthew Hrehirchuk © 2025. Licensed under the MIT License.