EnvironmentPlugin
EnvironmentPlugin is shorthand for defining selected process.env values with DefinePlugin. It reads environment variables when Rspack builds and replaces the corresponding process.env.* expressions in your bundled code.
Examples
Basic usage
Pass environment variable names as separate arguments or as an array. The following calls are equivalent:
Both configurations create definitions equivalent to:
If a requested variable is missing and has no default value, compilation fails with an EnvVariableNotDefinedError.
Using default values
Pass an object to provide a default value for each variable. A default is used only when the corresponding key is undefined in process.env when the build starts.
EnvironmentPlugin serializes default values with JSON.stringify before passing them to DefinePlugin. As a result, JSON-compatible defaults preserve their types: the false default above is injected as a boolean rather than a string.
Use undefined for a variable that must be provided during the build. If it is missing, compilation fails. Use null to provide an optional variable with a null fallback.
For example, suppose entry.js contains:
If NODE_ENV=production is set for the build and DEBUG is unset, the replacements are equivalent to:
If DEBUG=false is set and NODE_ENV is unset, the replacements are equivalent to:
Environment variables read from process.env are always strings. Setting DEBUG=false injects the string 'false', not the boolean false.
Using Git metadata
Default values can also be computed while loading the Rspack configuration. This example exposes the version and author date of the current Git commit:
Loading .env files
EnvironmentPlugin does not read .env files by itself. To load variables from a file, use a third-party plugin such as dotenv-webpack:
Only load values that are safe to embed in client-side code, because injected values can be read from the generated bundle.
Options
- Type:
Use either string form when every selected variable is required. Use the object form to provide default values; assigning undefined still marks that variable as required.
This page is adapted from webpack documentation under the CC BY 4.0, with modifications.

