Registry / devops / vite-plugin-environment

vite-plugin-environment

JSON →
library1.1.3jsnpmunverified

A Vite.js plugin (v1.1.3) that exposes environment variables to client code without requiring the VITE_ prefix. It acts as a shorthand for Vite's define option, similar to webpack's EnvironmentPlugin. Supports loading prefixed variables, custom define targets (import.meta.env), default values, optional/null handling, and .env file loading (on by default). Currently stable with monthly releases. Differentiates from Vite's built-in import.meta.env by allowing process.env usage and arbitrary variable names.

npm install vite-plugin-environment
INSTALL
IMPORT
SIG · VITE-PLUGIN-ENVIRO
V
vite-plugin-environment
devopsjavascriptv1.1.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

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

default
import EnvironmentPlugin from 'vite-plugin-environment'
const EnvironmentPlugin = require('vite-plugin-environment')
ESM-only package; requires import syntax
type
import type { EnvironmentPluginOptions } from 'vite-plugin-environment'
TypeScript types shipped; use import type for type-only usage
default (CommonJS)
import EnvironmentPlugin from 'vite-plugin-environment'
const EnvironmentPlugin = require('vite-plugin-environment').default
No CJS build; require() fails with ERR_REQUIRE_ESM. Use dynamic import() if needed.

Shows basic usage: expose two environment variables to client code as process.env.*

import { defineConfig } from 'vite'; import EnvironmentPlugin from 'vite-plugin-environment'; export default defineConfig({ plugins: [ EnvironmentPlugin(['API_KEY', 'DEBUG']), ], }); // In your client code: const apiKey = process.env.API_KEY; const debugMode = process.env.DEBUG === 'true';
Debug
Known issues
breakingRequires Vite >= 2.7; older versions may break or behave unexpectedly
fix
Upgrade Vite to at least 2.7
affects: <1.0.0
gotchaVariables exposed via this plugin are stringified; booleans/numbers become strings (e.g., DEBUG:'false' becomes string 'false')
fix
Explicitly parse booleans/numbers in client code: const debug = process.env.DEBUG === 'true'
affects: >=0.1.0
gotchaPassing 'all' as first argument loads ALL env variables; can unintentionally expose secrets if not filtered properly
fix
Use specific array of variable names or prefix option to limit exposure
affects: >=0.1.0
gotchaDefault values object: use null for optional, undefined for required. Mixing them up causes unintended failures
fix
Explicitly set null for optional variables, undefined (or omit) for required
affects: >=1.0.0
deprecatedNo deprecation warnings known in current version
affects: none
Errors
Common errors & fixes
Error: Cannot find module 'vite-plugin-environment'
Package not installed or missing from package.json
fix
Run `npm install -D vite-plugin-environment` or `yarn add -D vite-plugin-environment`
ReferenceError: process is not defined
Using process.env in browser context without polyfill; plugin expects Vite's define to work
fix
Ensure plugin is properly loaded in vite.config.js, and check that the variable is defined in process.env during build
TypeError: EnvironmentPlugin is not a function
Using require() on an ESM-only package
fix
Use import statement: `import EnvironmentPlugin from 'vite-plugin-environment'`
vite-plugin-environment: Environment variable "API_KEY" is not defined
Variable is missing from process.env and has no default (undefined)
fix
Define the variable in your .env file or provide a default value in EnvironmentPlugin({ API_KEY: 'default' })
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies
viterequiredPeer dependency required to run as Vite plugin; must be >= 2.7
Agent activity
2 hits · last 30 days
node
2
Resources
vite-plugin-environment — npm install vite-plugin-environment · libregistry