{"id":22651,"library":"vite-plugin-environment","title":"vite-plugin-environment","description":"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.","status":"active","version":"1.1.3","language":"javascript","source_language":"en","source_url":"https://github.com/ElMassimo/vite-plugin-environment","tags":["javascript","env","environment","variables","vite","vite-plugin","vitejs","typescript"],"install":[{"cmd":"npm install vite-plugin-environment","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-environment","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-environment","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required to run as Vite plugin; must be >= 2.7","package":"vite","optional":false}],"imports":[{"note":"ESM-only package; requires import syntax","wrong":"const EnvironmentPlugin = require('vite-plugin-environment')","symbol":"default","correct":"import EnvironmentPlugin from 'vite-plugin-environment'"},{"note":"TypeScript types shipped; use import type for type-only usage","wrong":null,"symbol":"type","correct":"import type { EnvironmentPluginOptions } from 'vite-plugin-environment'"},{"note":"No CJS build; require() fails with ERR_REQUIRE_ESM. Use dynamic import() if needed.","wrong":"const EnvironmentPlugin = require('vite-plugin-environment').default","symbol":"default (CommonJS)","correct":"import EnvironmentPlugin from 'vite-plugin-environment'"}],"quickstart":{"code":"import { defineConfig } from 'vite';\nimport EnvironmentPlugin from 'vite-plugin-environment';\n\nexport default defineConfig({\n  plugins: [\n    EnvironmentPlugin(['API_KEY', 'DEBUG']),\n  ],\n});\n\n// In your client code:\nconst apiKey = process.env.API_KEY;\nconst debugMode = process.env.DEBUG === 'true';","lang":"typescript","description":"Shows basic usage: expose two environment variables to client code as process.env.*"},"warnings":[{"fix":"Upgrade Vite to at least 2.7","message":"Requires Vite >= 2.7; older versions may break or behave unexpectedly","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Explicitly parse booleans/numbers in client code: const debug = process.env.DEBUG === 'true'","message":"Variables exposed via this plugin are stringified; booleans/numbers become strings (e.g., DEBUG:'false' becomes string 'false')","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use specific array of variable names or prefix option to limit exposure","message":"Passing 'all' as first argument loads ALL env variables; can unintentionally expose secrets if not filtered properly","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Explicitly set null for optional variables, undefined (or omit) for required","message":"Default values object: use null for optional, undefined for required. Mixing them up causes unintended failures","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"","message":"No deprecation warnings known in current version","severity":"deprecated","affected_versions":"none"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Run `npm install -D vite-plugin-environment` or `yarn add -D vite-plugin-environment`","cause":"Package not installed or missing from package.json","error":"Error: Cannot find module 'vite-plugin-environment'"},{"fix":"Ensure plugin is properly loaded in vite.config.js, and check that the variable is defined in process.env during build","cause":"Using process.env in browser context without polyfill; plugin expects Vite's define to work","error":"ReferenceError: process is not defined"},{"fix":"Use import statement: `import EnvironmentPlugin from 'vite-plugin-environment'`","cause":"Using require() on an ESM-only package","error":"TypeError: EnvironmentPlugin is not a function"},{"fix":"Define the variable in your .env file or provide a default value in EnvironmentPlugin({ API_KEY: 'default' })","cause":"Variable is missing from process.env and has no default (undefined)","error":"vite-plugin-environment: Environment variable \"API_KEY\" is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}