vite-inject-env is a build tool designed to address a common challenge in modern web application deployment, particularly for Vite-based projects. It enables the injection of environment variables *after* the static files have been built, rather than requiring them at build time. This allows developers to create a single, immutable build artifact that can be deployed to multiple environments (e.g., development, staging, production) with different configurations, promoting a more efficient and reliable CI/CD workflow. The current stable version is 2.1.0, and while a specific release cadence isn't published, the presence of distinct v1.0 and v2.0 documentation suggests major versions are released to incorporate significant architectural or feature updates. A key differentiator is its synchronous availability of environment variables in the browser without the need for asynchronous fetching, resolving a limitation often encountered with other post-build environment variable solutions.
npm install vite-inject-envVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `vite-inject-env` into a Vite project, showing the necessary `index.html` modification, the `env.ts` file setup, and how to use the `env` object in a component. It also illustrates the post-build CLI command for injecting variables.
Consult the 'v1.0 guide' linked in the README for migration details. The usage patterns, especially for the custom `env.js`/`env.ts` file, have likely changed.
Be mindful of the order of precedence. If you intend for `.env` files to be the primary source, ensure no conflicting variables are passed through the command line during the injection step.
Use the `-v` or `--var` option with the `vite-inject-env set` command to specify an alternative variable name (e.g., `npx vite-inject-env set -v myCustomEnv`). Remember to update your `env.js`/`env.ts` file to use this new window property.
Ensure the script tag is present in your main HTML file and points to the correct path where `vite-inject-env` will generate the `env.js` file (defaulting to `/env.js` relative to your build output).
Ensure you have created the `env.js` (or `env.ts`) file as specified in the usage instructions, added `<script src='/env.js'></script>` to `index.html`, and executed `npx vite-inject-env set` after building your application.
Update the `EnvType` interface in your `env.ts` file to include all expected environment variables and their types. For example, add `VITE_MY_VARIABLE: string;` to the interface.
No dependency data recorded yet.