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-typed-env

v1.3.0

Published

`next-typed-env` is a utility that enhances Next.js applications by enabling type-safe handling and validation of environment variables. It generates TypeScript files with typed exports for both client and server environments.

Downloads

14

Readme

next-typed-env

next-typed-env is a utility that enhances Next.js applications by enabling type-safe handling and validation of environment variables. It generates TypeScript files with typed exports for both client and server environments.

Features

  • Validates environment variables against a predefined schema
  • Generates TypeScript files with typed exports
  • Supports both client and server environments
  • Reduces runtime errors by ensuring consistent access to environment variables

Installation

Use npm to install next-typed-env:

npm install next-typed-env

or use yarn:

yarn add next-typed-env

Usage

Define Environment Variables in .env File

Create a .env file in the root directory of your Next.js project and define environment variables:

NEXT_PUBLIC_API_URL=https://api.example.com
SECRET_KEY=mysecretkey
NODE_ENV=development

Define a Schema for Environment Variables

Define a schema for your environment variables using Zod. Use the withTypedEnv function in your Next.js configuration file to integrate next-typed-env.

// next.config.js
import { withTypedEnv } from "next-typed-env";
import { z } from "zod";

const envSchema = z.object({
  NEXT_PUBLIC_API_URL: z.string(),
  SECRET_KEY: z.string(),
  NODE_ENV: z.enum(["development", "production", "test"]),
});

module.exports = withTypedEnv(
  {
    // ...other Next.js config options
  },
  envSchema
);

Generated TypeScript Files

next-typed-env will generate TypeScript files in an /env directory.

// ./env/env.client.ts
export const NEXT_PUBLIC_API_URL = process.env.NEXT_PUBLIC_API_URL as "https://api.example.com";

export const NODE_ENV = process.env.NODE_ENV as "development" | "production" | "test";

Example generated file (env.server.ts):

// ./env/env.server.ts
export const SECRET_KEY = process.env.SECRET_KEY as "mysecretkey";

Use the Generated File in Your Code

Import the environment variables directly from the generated file. This will provide you with type safety:

// example-component.tsx
import { NEXT_PUBLIC_API_URL, NODE_ENV } from "../env/env.client";

console.log(NEXT_PUBLIC_API_URL); // Typed as string

if (NODE_ENV === "production") {
  console.log("You are in production mode.");
} else if (NODE_ENV === "development") {
  console.log("You are in development mode.");
}
// example-server.ts
import { SECRET_KEY } from "../env/env.server";

console.log(SECRET_KEY); // Typed as string

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT