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

@ibnushahraa/vite-plugin-dotenv-guard

v0.4.3

Published

Vite plugin for dotenv-guard - load and validate environment variables

Readme

@ibnushahraa/vite-plugin-dotenv-guard

npm version npm downloads license coverage

🔐 Vite plugin for dotenv-guard - load and validate environment variables with auto-mode detection.


✨ Features

  • 🔄 Auto Mode Detection → Automatically loads .env.{mode} based on Vite mode
  • 🔒 Encryption Support → Auto-decrypts encrypted values (AES-256-GCM)
  • 🎯 Selective Encryption → Config-based encryption via env.enc.json
  • Schema Validation → Enforce required keys, regex patterns, enums (same as core)
  • Zero Config → Works out of the box with Vite modes
  • 🎯 Type-Safe → Full TypeScript support
  • 📦 Zero Native Deps → Built-in crypto, no keytar required

📦 Installation

npm install @ibnushahraa/vite-plugin-dotenv-guard

🚀 Usage

Basic Setup

// vite.config.js
import { defineConfig } from 'vite';
import dotenvGuard from '@ibnushahraa/vite-plugin-dotenv-guard';

export default defineConfig({
  plugins: [
    dotenvGuard()
  ]
});

This automatically loads:

  • .env.development → when running npm run dev (development mode)
  • .env.production → when running npm run build (production mode)
  • .env.{custom} → when running vite --mode custom

With Schema Validation

Create env.schema.json:

{
  "VITE_API_URL": {
    "required": true,
    "regex": "^https?://"
  },
  "VITE_API_KEY": {
    "required": true
  },
  "VITE_ENV": {
    "required": true,
    "enum": ["development", "staging", "production"]
  }
}

Enable validator:

// vite.config.js
import dotenvGuard from '@ibnushahraa/vite-plugin-dotenv-guard';

export default defineConfig({
  plugins: [
    dotenvGuard({
      validator: true
    })
  ]
});

If validation fails, build/dev server will exit with error details.


⚙️ Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | path | string | undefined | Custom .env file path (auto-detects .env.{mode} if not set) | | validator | boolean | false | Enable schema validation | | schema | string | 'env.schema.json' | Schema file path |

Note: Encryption is always enabled. Plugin auto-decrypts encrypted values at build-time (read-only, no file modification).


📚 Examples

With Validation

dotenvGuard({
  validator: true
})

Note:

  • Encryption is always enabled (auto-decrypts encrypted values)
  • Selective encryption is configured via CLI: npx dotenv-guard init enc
  • To use plaintext .env, run: npx dotenv-guard decrypt first

Custom Schema Path

dotenvGuard({
  validator: true,
  schema: 'config/env.schema.json'
})

Custom File Path

dotenvGuard({
  path: '.env.custom',
  validator: true
})

TypeScript

// vite.config.ts
import { defineConfig } from 'vite';
import dotenvGuard from '@ibnushahraa/vite-plugin-dotenv-guard';

export default defineConfig({
  plugins: [
    dotenvGuard({
      validator: true,
      schema: 'env.schema.json'
    })
  ]
});

📋 Schema Validation Rules

Same format as core package:

{
  "VARIABLE_NAME": {
    "required": true,        // Boolean (default: true)
    "regex": "^pattern$",    // String regex pattern
    "enum": ["val1", "val2"] // Array of allowed values
  }
}

Example:

{
  "VITE_API_URL": {
    "required": true,
    "regex": "^https?://.*"
  },
  "VITE_LOG_LEVEL": {
    "required": false,
    "enum": ["debug", "info", "warn", "error"]
  }
}

🔄 Mode Detection

Plugin automatically detects Vite mode:

npm run dev              # Loads .env.development
npm run build            # Loads .env.production
vite --mode staging      # Loads .env.staging

🆚 Comparison

❌ Without Plugin

// Manual dotenv loading
import dotenv from 'dotenv';
dotenv.config({ path: `.env.${process.env.NODE_ENV}` });

export default defineConfig({
  // ... config
});

✅ With Plugin

import dotenvGuard from '@ibnushahraa/vite-plugin-dotenv-guard';

export default defineConfig({
  plugins: [dotenvGuard({ validator: true })]
});

🛡️ Best Practices

  • ✅ Use VITE_ prefix for client-side variables
  • ✅ Keep .env.local and .env.*.local in .gitignore
  • ✅ Commit .env.development and .env.production templates
  • ✅ Enable validator: true to catch missing variables early

⚠️ Important Notes

  • Auto-Decryption: Always enabled, auto-decrypts encrypted values using AES-256-GCM (via core package)
  • Selective Encryption: Configure via npx dotenv-guard init enc (creates env.enc.json)
  • Read-Only: Plugin only decrypts at build-time, never modifies .env files
  • Master Key: Encryption key stored in ~/.dotenv-guard/master.key (auto-generated)
  • Vite-Only: Designed specifically for Vite. For other build tools, use @ibnushahraa/dotenv-guard

📖 Related


📄 License

MIT © 2025