react-inject-env is a specialized build-time utility for React applications that enables the injection of environment variables *after* the static files have been built. This addresses a common challenge in React development where `process.env` variables are typically bundled into the application at build time, preventing a single build artifact from being used across multiple deployment environments without rebuilding. The current stable version is 2.1.0. The library works by generating a `env.js` file (or custom name) in your build output, which then exposes variables via `window.env` at runtime. Key differentiators include its ability to modify environment variables without rebuilding the application, synchronous access to these variables (unlike some asynchronous loading solutions), and straightforward integration into CI/CD pipelines and Docker workflows. It supports `.env` files and command-line arguments for variable definition, with command-line variables taking precedence. This approach significantly simplifies multi-environment deployments.
npm install react-inject-envVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure a React application to use `react-inject-env`. It includes modifying `index.html`, creating a `env.ts` file, updating an `App` component to consume the `env` object, and then shows the terminal commands to install, build, and inject variables post-build.
Refer to the v2.x documentation for updated setup instructions, specifically the steps for modifying `index.html` and creating an `env.js`/`env.ts` file. Replace all `process.env.YOUR_VAR` with `env.YOUR_VAR` in your application code.
Ensure `npx react-inject-env set` is executed as part of your deployment script *after* `npm run build`. Verify that `public/index.html` contains the script tag pointing to your environment file (e.g., `/env.js`).
Be aware of the precedence order. For consistent behavior, define critical variables either solely in `.env` files or solely via command-line arguments in your CI/CD, avoiding conflicts where possible.
Use the `-v` or `--var` option with `npx react-inject-env set` to specify a different global variable name, e.g., `npx react-inject-env set --var myAppEnv` and update your `env.js`/`env.ts` file accordingly (e.g., `export const env = { ...process.env, ...window.myAppEnv }`).Ensure `import { env } from './env'` is present in your component files. Verify `src/env.js` or `src/env.ts` exists and is correctly structured. Check `public/index.html` for `<script src='/env.js'></script>`.Run `npm install react-inject-env --save-dev` or `yarn add react-inject-env --dev`. If running in Docker, ensure `npm install` runs after `COPY . /app` to install dependencies.
Confirm `npx react-inject-env set` was executed after building and before serving. Ensure the `script` tag for `env.js` is present in `index.html` and points to the correct path relative to your served static files.
No dependency data recorded yet.