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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-check-env

v1.2.17

Published

A Vite plugin to check that required environment variables are available at build time. Scans your code for calls to a specified function and ensures the environment variables referenced in those calls are defined.

Readme

Vite: Assert Environment Variables Exist During Build

A Vite plugin to automatically ensure all environment variables used in your project exist at build time.

It does this in a very simple way by scanning your code for calls to a specified function (default: requireEnv) and checking that all environment variables referenced in those calls are defined in your environment.

It's simple, and it works. Here's an example of it being used in a project.

Features

  • Build-time validation: Catches missing environment variables before deployment
  • No maintenance overhead: No need to maintain a separate list of required environment variables
  • Configurable function name: Customize the function name that triggers environment variable checks
  • TypeScript support: Works with JavaScript, TypeScript, JSX, and TSX files

How it works

The plugin scans your code for calls to a specified function (default: requireEnv) and ensures that all environment variables referenced in those calls are defined in your environment.

Installation

pnpm i -D vite-plugin-check-env

Basic Usage

  1. Update vite.config.ts:
import { defineConfig } from 'vite'
import { checkEnv } from 'vite-plugin-check-env'

export default defineConfig({
  plugins: [
    checkEnv(),
  ],
})
  1. In your code, use the requireEnv function to access environment variables:
// This will be checked at build time
const apiUrl = requireEnv('API_URL')
const secretKey = requireEnv('SECRET_KEY')
  1. Make sure your environment variables are defined in your .env file or environment:
API_URL=https://api.example.com
SECRET_KEY=your-secret-key
  1. Run your build as usual:
npm run dev
# or
npm run build

If any environment variables are missing, the build will fail with a clear error message.

Configuration

Custom Method Name

You can configure the function name that the plugin looks for:

import { defineConfig } from 'vite'
import { checkEnv } from 'vite-plugin-check-env'

export default defineConfig({
  plugins: [
    checkEnv({
      methodName: 'getEnv' // Look for getEnv() calls instead of requireEnv()
    }),
  ],
})

Then in your code:

const apiUrl = getEnv('API_URL')

Example

Here's a complete example of how to use the plugin:

vite.config.ts

import { defineConfig } from 'vite'
import { checkEnv } from 'vite-plugin-check-env'

export default defineConfig({
  plugins: [
    checkEnv(),
  ],
})

src/config.ts

// These environment variables will be validated at build time
export const config = {
  apiUrl: requireEnv('VITE_API_URL'),
  appName: requireEnv('VITE_APP_NAME'),
  version: requireEnv('VITE_VERSION'),
}

.env

VITE_API_URL=https://api.example.com
VITE_APP_NAME=My App
VITE_VERSION=1.0.0

If you forget to define any of these variables, you'll get a build error like:

Missing environment variables for requireEnv: VITE_VERSION

MIT License