{"id":22650,"library":"vite-plugin-env","title":"vite-plugin-env","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/zouloux/deuspi","tags":["javascript","vite","plugin","custom","env","envs","variable","typescript"],"install":[{"cmd":"npm install vite-plugin-env","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-env","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-env","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Named export only. Default import will fail.","wrong":"import env from 'vite-plugin-env'","symbol":"env","correct":"import { env } from 'vite-plugin-env'"},{"note":"CommonJS require using destructuring. The plugin is ESM-first but works with CJS via dynamic require.","wrong":"const vitePluginEnv = require('vite-plugin-env')","symbol":"vite-plugin-env (package)","correct":"const { env } = require('vite-plugin-env')"},{"note":"TypeScript users may want to type the plugin return value as Plugin, but vite-plugin-env does not export a Plugin type directly.","wrong":"import { env } from 'vite-plugin-env' // no type import","symbol":"Plugin type","correct":"import type { Plugin } from 'vite'; import { env } from 'vite-plugin-env'"}],"quickstart":{"code":"// vite.config.ts\nimport { defineConfig } from 'vite';\nimport { env } from 'vite-plugin-env';\n\nexport default defineConfig({\n  plugins: [\n    env({\n      MY_CUSTOM_ENV: 'Hello from vite config',\n      YOUR_SECRET_KEY: process.env.YOUR_SECRET_KEY ?? ''\n    })\n  ]\n});\n\n// src/index.ts\nconsole.log(import.meta.env.MY_CUSTOM_ENV); // 'Hello from vite config'\nconsole.log(import.meta.env.YOUR_SECRET_KEY); // value of env var or ''","lang":"typescript","description":"Shows how to use the env plugin to inject custom variables into import.meta.env, including a fallback for secrets via process.env."},"warnings":[{"fix":"Replace import { env } from 'vite-plugin-stachtml' with import { env } from 'vite-plugin-env'.","message":"The README example mistakenly imports from 'vite-plugin-stachtml' instead of 'vite-plugin-env'.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure env({ ... }) returns a valid Vite plugin object.","message":"The env function must be called as a function, not used as an object. Misuse will cause Vite to throw Plugin type error.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use process.env for server-side variables or define plugin for universal values.","message":"Variables set via env plugin are only available in client code, not in Node.js environment (e.g., vite.config.ts itself).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use uppercase variable names (e.g., MY_VAR).","message":"Variable names must be uppercase or Vite may strip them due to security filters on import.meta.env.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Ensure you call env({ ... }) and import { env } from 'vite-plugin-env'.","cause":"env() not called as a function or imported incorrectly.","error":"MissingPluginError: The plugin 'env' was not found or has invalid shape."},{"fix":"Add MY_VAR to the env({...}) object with a default value.","cause":"Variable not defined in env plugin config.","error":"[vite] Internal server error: import.meta.env.MY_VAR is undefined"},{"fix":"Run npm install vite-plugin-env --save-dev","cause":"Package not installed.","error":"Error: Cannot find module 'vite-plugin-env'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}