Registry / web-framework / vite-plugin-env

vite-plugin-env

JSON →
library1.0.1jsnpmunverified

A Vite plugin that injects custom environment variables into the client bundle via import.meta.env. Current stable version is 1.0.1, released with no documented release cadence. It differentiates from Vite's built-in env handling by allowing custom variables defined directly in the Vite config, rather than relying on .env files or process.env. Supports TypeScript types but its API surface is minimal. Caution: The README example contains a typo referencing 'vite-plugin-stachtml' which will cause import errors.

npm install vite-plugin-env
INSTALL
IMPORT
SIG · VITE-PLUGIN-ENV
V
vite-plugin-env
web-frameworkjavascriptv1.0.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–223 runs
build_error
glibc
node 18–223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

env
✓ import { env } from 'vite-plugin-env'
✗ import env from 'vite-plugin-env'
Named export only. Default import will fail.
vite-plugin-env (package)
✓ const { env } = require('vite-plugin-env')
✗ const vitePluginEnv = require('vite-plugin-env')
CommonJS require using destructuring. The plugin is ESM-first but works with CJS via dynamic require.
Plugin type
✓ import type { Plugin } from 'vite'; import { env } from 'vite-plugin-env'
✗ import { env } from 'vite-plugin-env' // no type import
TypeScript users may want to type the plugin return value as Plugin, but vite-plugin-env does not export a Plugin type directly.

Shows how to use the env plugin to inject custom variables into import.meta.env, including a fallback for secrets via process.env.

// vite.config.ts import { defineConfig } from 'vite'; import { env } from 'vite-plugin-env'; export default defineConfig({ plugins: [ env({ MY_CUSTOM_ENV: 'Hello from vite config', YOUR_SECRET_KEY: process.env.YOUR_SECRET_KEY ?? '' }) ] }); // src/index.ts console.log(import.meta.env.MY_CUSTOM_ENV); // 'Hello from vite config' console.log(import.meta.env.YOUR_SECRET_KEY); // value of env var or ''
Debug
Known issues
gotchaThe README example mistakenly imports from 'vite-plugin-stachtml' instead of 'vite-plugin-env'.
fix
Replace import { env } from 'vite-plugin-stachtml' with import { env } from 'vite-plugin-env'.
affects: >=0.0.0
breakingThe env function must be called as a function, not used as an object. Misuse will cause Vite to throw Plugin type error.
fix
Ensure env({ ... }) returns a valid Vite plugin object.
affects: >=1.0.0
gotchaVariables set via env plugin are only available in client code, not in Node.js environment (e.g., vite.config.ts itself).
fix
Use process.env for server-side variables or define plugin for universal values.
affects: >=1.0.0
gotchaVariable names must be uppercase or Vite may strip them due to security filters on import.meta.env.
fix
Use uppercase variable names (e.g., MY_VAR).
affects: >=1.0.0
Errors
Common errors & fixes
MissingPluginError: The plugin 'env' was not found or has invalid shape.
env() not called as a function or imported incorrectly.
fix
Ensure you call env({ ... }) and import { env } from 'vite-plugin-env'.
[vite] Internal server error: import.meta.env.MY_VAR is undefined
Variable not defined in env plugin config.
fix
Add MY_VAR to the env({...}) object with a default value.
Error: Cannot find module 'vite-plugin-env'
Package not installed.
fix
Run npm install vite-plugin-env --save-dev
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
vite-plugin-env — npm install vite-plugin-env · libregistry