dotenv-webpack-plugin
is a webpack plugin that enables consumers to load environment variables from dotenv files. This plugin simplifies the process of managing environment-specific configurations in your webpack projects.
- Loads environment variables from dotenv files
- Provides a convenient way to manage environment-specific configurations
- Fully configurable via an options API
Install dotenv-webpack-plugin
using npm:
npm install dotenv-webpack-plugin --save-dev
or using yarn:
yarn add dotenv-webpack-plugin --dev
or using pnpm:
pnpm add dotenv-webpack-plugin --save-dev
To use dotenv-webpack-plugin
, follow these steps:
-
Create a
.env
file in the root directory of your project. Add each environment variable on a new lines in the form ofPUBLIC_NAME=VALUE
. By default only variables that are prefixed withPUBLIC_
will be exposed to webpack. The prefix can be changed by passing theenvVarPrefix
option to the plugin. -
Import
dotenv-webpack-plugin
in your webpack configuration file:const DotenvWebpackPlugin = require("dotenv-webpack-plugin");
-
Add an instance of DotenvWebpackPlugin to your webpack plugins:
module.exports = { // Your webpack configuration options... plugins: [new DotenvWebpackPlugin()], };
DotenvWebpackPlugin accepts the following configuration options:
-
envFiles
: An array of dotenv files to load. By default, DotenvWebpackPlugin will look for the following files in the root directory of your project:.env.[mode].local
.env.local
.env.[mode]
.env
.env.example
The
[mode]
placeholder will be replaced with the current webpack mode. For example, if the current webpack mode isdevelopment
, DotenvWebpackPlugin will look for the following files:.env.development.local
.env.local
.env.development
.env
.env.example
If the same variable is defined in multiple files, the value from the file with the highest precedence will be used. The precedence order is same as the order of files listed above.
While passing an array of dotenv files, the path towards the right of the array will have the highest precedence. For example, if you pass
["./.env", "./.env.local"]
, the value from.env.local
will be used if the same variable is defined in both files. -
envVarPrefix
: The prefix to use when loading environment variables. By default, DotenvWebpackPlugin will look for variables prefixed withPUBLIC_
. -
prefixes
: An array of prefixes to prepend to the names of environment variables. By default, DotenvWebpackPlugin will prependprocess.env.
andimport.meta.env.
to the names of environment variables. -
allowEmptyValues
: A boolean value indicating whether to allow empty values. By default this value is set tofalse
, DotenvWebpackPlugin will throw an error if an environment variable is defined without a value.
You can pass these options when creating an instance of DotenvWebpackPlugin:
new DotenvWebpackPlugin({
envFiles: ["./.env", "./.env.local"],
prefixes: ["process.env.", "import.meta.env."],
envVarPrefix: "PUBLIC_",
allowEmptyValues: false,
});