Registry / devops / vite-envs

vite-envs

JSON →
library4.6.2jsnpmunverified

Vite plugin (v4.6.2, actively maintained) that enables dynamic injection of environment variables at container startup rather than build time. Designed for Dockerized Vite apps, it generates a shell script during build that populates import.meta.env and index.html placeholders (e.g., %FOO%) at runtime. Unlike standard Vite which requires rebuilding for env changes, vite-envs allows deploying a single Docker image configurable via --env flags. Supports type-safe imports with hot reload, optional EJS in HTML, and explicit variable declaration. Does not require the VITE_ prefix.

npm install vite-envs
INSTALL
IMPORT
SIG · VITE-ENVS
V
vite-envs
devopsjavascriptv4.6.2
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.

viteEnvs
import { viteEnvs } from 'vite-envs'
const viteEnvs = require('vite-envs')
ESM-only; CJS require() fails in modern Vite configs.
default import
import viteEnvs from 'vite-envs'
import { default } from 'vite-envs'
viteEnvs is also the default export; both syntaxes work.
defineEnvVars
import { defineEnvVars } from 'vite-envs'
Utility for type-safe env variable declarations; available since v4.

Minimal Vite config using viteEnvs plugin. Shows declaration, .env fallback, and runtime access via import.meta.env.

// vite.config.ts import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import { viteEnvs } from 'vite-envs'; export default defineConfig({ plugins: [ react(), viteEnvs({ // Declare env vars (optional, but required for type generation) declaration: { FOO: 'VITE_FOO', // default value BAR: 'default_bar' } }) ] }); // .env file (if not using declaration) VITE_FOO=hello BAR=world // In your TypeScript code: console.log(import.meta.env.FOO); // 'hello' at runtime console.log(import.meta.env.BAR); // 'world' // Docker usage: // docker run --env FOO="xyz" --env BAR="abc" my-image
Debug
Known issues
breakingvite-envs v4 generates a shell script (vite-envs.sh) rather than directly modifying the bundle. Previous versions (<4) used a different injection mechanism.
fix
Update Dockerfile to run ./vite-envs.sh before starting server (see README).
affects: <4.0.0
gotchaEnvironment variables not declared in .env (or .env.declaration) will NOT be injected at runtime, even if present in the host environment. This is a security feature.
fix
List all expected variables in a .env file (or use the declaration option in vite.config.ts).
affects: >=4.0.0
gotchaThe VITE_ prefix is NOT required, but if you omit it, you must ensure the variable is declared. Mixing VITE_ and non-VITE_ variables can cause confusion.
fix
Use explicit declaration for all env vars to avoid surprises.
affects: >=4.0.0
deprecatedIn v4.6.0+, you can overwrite the global name via the plugin option nameOfTheGlobalOld. This may change in future versions.
fix
Avoid relying on internal global names; use the default behavior.
affects: >=4.6.0
Errors
Common errors & fixes
Cannot find module 'vite-envs' or its corresponding type declarations.
Package not installed or TypeScript config missing module resolution for vite-envs.
fix
npm install vite-envs --save-dev && ensure tsconfig.json includes 'node' in moduleResolution or 'moduleResolution': 'bundler'.
TypeError: viteEnvs is not a function
Importing viteEnvs incorrectly (e.g., as default export when using named import syntax).
fix
Use 'import { viteEnvs } from 'vite-envs'; or 'import viteEnvs from 'vite-envs';'
env variable FOO is not being injected at runtime
FOO not listed in .env file or plugin declaration, so vite-envs.sh does not include it.
fix
Add FOO to your .env file (even with a dummy value) or use the declaration option in viteEnvs().
Upgrade
Version history
4.6.2latest on npm
Audit
Dependencies
viterequiredPeer dependency; plugin runs as Vite plugin.
Agent activity
8 hits · last 30 days
node
8
Resources
vite-envs — npm install vite-envs · libregistry