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 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-zodenv

v0.3.0

Published

Safe environment variables for Next.js, powered by Zod

Downloads

47

Readme

next-zodenv

next-zodenv makes dealing with environment variables in Next.js safer. Its concept is heavily inspired by envsafe created by KATT, but there are some nuances; you define a schema for your process.env with Zod's constructs! In addition, because it's developed to work with Next.js in mind, integrating next-zodenv with Next.js is a piece of cake (it can be used with other environments, though).

Here are the basic ideas of next-zodenv:

  • 💎 Express environment variables declaratively using Zod
  • ✅ Validate that the specified environment variables are not missing on build time
  • 🪄 Transform environment variables to the type your application expects
  • 🤝 Work on Node.js and the browser

Setup

npm install next-zodenv zod

Usage

Suppose that you have the following environment variables:

FOO="server_only_variable"
PORT=3000
API_URL="https://example.com/api"
NEXT_PUBLIC_VAR="public_variable"

You can define a schema for your environment variables:

import { zenv } from 'next-zodenv'
import { z } from 'zod'

const env = zenv({
  FOO: z.string(),
  PORT: z.preprocess(Number, z.number().int().gte(1).lte(65535)),
  API_URL: z.string().url(),
})

env.FOO             // string
env.PORT            // number (1-65535)
env.API_URL         // string (URL)
env.NEXT_PUBLIC_VAR // undefined

Note that types other than string must be transformed with z.preprocess beforehand. This is because environment variables are always string and we need to transform them to the type Zod's schema expects.

For simple cases like the above, next-zodenv offers built-in validators defined using Zod's constructs:

import { zenv, str, port, url } from 'next-zodenv'

const env = zenv({
  FOO: str(),
  PORT: port(),
  API_URL: url(),
})

The complete list of built-in validators is as follows:

Validator | Zod schema --- | --- str() | z.string() bool() | z.preprocess((val) => val === 'true', z.boolean()) num() | z.preprocess(Number, z.number()) port() | z.preprocess(Number, z.number().int().min(1).max(65535)) url() | z.string().url() email() | z.string().email()

Next.js

In order to expose environment variables to the browser in Next.js, you need to pass process.env.NEXT_PUBLIC_* to the value prop like this:

const env = zenv(
  {
    NEXT_PUBLIC_VAR: {
      zodType: z.string(),
      value: process.env.NEXT_PUBLIC_VAR,
    },
  },
)

env.NEXT_PUBLIC_VAR // available in the browser

To validate on build time and stop the build process if there are missing environment variables, load your schema in next.config.js:

// next.config.js
const { env } = require('./env/server.js');

// ...