|
| 1 | +--- |
| 2 | +title: Dotenv |
| 3 | +sort: 22 |
| 4 | +contributors: |
| 5 | + - xiaoxiaojx |
| 6 | +related: |
| 7 | + - title: dotenv |
| 8 | + url: https://github.com/motdotla/dotenv |
| 9 | + - title: dotenv-expand |
| 10 | + url: https://github.com/motdotla/dotenv-expand |
| 11 | + - title: Vite Environment Variables |
| 12 | + url: https://vitejs.dev/guide/env-and-mode.html |
| 13 | +--- |
| 14 | + |
| 15 | +The `dotenv` option enables webpack's built-in environment variable loading from `.env` files. This provides a convenient way to manage environment-specific configuration without external plugins. |
| 16 | + |
| 17 | +## `dotenv` |
| 18 | + |
| 19 | +`boolean` `object` |
| 20 | + |
| 21 | +Enable and configure the built-in Dotenv plugin to load environment variables from `.env` files. |
| 22 | + |
| 23 | +**webpack.config.js** |
| 24 | + |
| 25 | +```javascript |
| 26 | +module.exports = { |
| 27 | + //... |
| 28 | + dotenv: true, |
| 29 | +}; |
| 30 | +``` |
| 31 | + |
| 32 | +Setting `dotenv` to `true` enables the plugin with default options. For custom configuration, pass an options object: |
| 33 | + |
| 34 | +```javascript |
| 35 | +module.exports = { |
| 36 | + //... |
| 37 | + dotenv: { |
| 38 | + prefix: 'WEBPACK_', |
| 39 | + dir: true, |
| 40 | + template: ['.env', '.env.local', '.env.[mode]', '.env.[mode].local'], |
| 41 | + }, |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +## Options |
| 46 | + |
| 47 | +### `prefix` |
| 48 | + |
| 49 | +`string` `string[]` |
| 50 | + |
| 51 | +**Default:** `'WEBPACK_'` |
| 52 | + |
| 53 | +Only expose environment variables that start with the specified prefix(es). This prevents accidental exposure of sensitive variables. |
| 54 | + |
| 55 | +**webpack.config.js** |
| 56 | + |
| 57 | +```javascript |
| 58 | +module.exports = { |
| 59 | + //... |
| 60 | + dotenv: { |
| 61 | + prefix: 'APP_', // Only expose APP_* variables |
| 62 | + }, |
| 63 | +}; |
| 64 | +``` |
| 65 | + |
| 66 | +**Multiple prefixes:** |
| 67 | + |
| 68 | +```javascript |
| 69 | +module.exports = { |
| 70 | + //... |
| 71 | + dotenv: { |
| 72 | + prefix: ['APP_', 'CONFIG_'], // Expose both APP_* and CONFIG_* variables |
| 73 | + }, |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +T> For security reasons, an empty string `''` is not allowed as a prefix, as it would expose all environment variables. |
| 78 | + |
| 79 | +### `dir` |
| 80 | + |
| 81 | +`boolean` `string` |
| 82 | + |
| 83 | +**Default:** `true` |
| 84 | + |
| 85 | +The directory from which `.env` files are loaded. |
| 86 | + |
| 87 | +- `true` - Load from the project root (context) |
| 88 | +- `false` - Disable `.env` file loading |
| 89 | +- `string` - Relative path from project root or absolute path |
| 90 | + |
| 91 | +**webpack.config.js** |
| 92 | + |
| 93 | +```javascript |
| 94 | +module.exports = { |
| 95 | + //... |
| 96 | + dotenv: { |
| 97 | + dir: './config', // Load from ./config directory |
| 98 | + }, |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +**Disable loading:** |
| 103 | + |
| 104 | +```javascript |
| 105 | +module.exports = { |
| 106 | + //... |
| 107 | + dotenv: { |
| 108 | + dir: false, // Only use process.env variables |
| 109 | + }, |
| 110 | +}; |
| 111 | +``` |
| 112 | + |
| 113 | +### `template` |
| 114 | + |
| 115 | +`string[]` |
| 116 | + |
| 117 | +**Default:** `['.env', '.env.local', '.env.[mode]', '.env.[mode].local']` |
| 118 | + |
| 119 | +Template patterns for `.env` file names. Use `[mode]` as a placeholder for the webpack mode (e.g., `development`, `production`). |
| 120 | + |
| 121 | +Files are loaded in the order specified, with later files overriding earlier ones. |
| 122 | + |
| 123 | +**webpack.config.js** |
| 124 | + |
| 125 | +```javascript |
| 126 | +module.exports = { |
| 127 | + //... |
| 128 | + mode: 'production', |
| 129 | + dotenv: { |
| 130 | + template: ['.env', '.env.production'], // Only load these two files |
| 131 | + }, |
| 132 | +}; |
| 133 | +``` |
| 134 | + |
| 135 | +**Custom patterns:** |
| 136 | + |
| 137 | +```javascript |
| 138 | +module.exports = { |
| 139 | + //... |
| 140 | + dotenv: { |
| 141 | + template: [ |
| 142 | + '.env', |
| 143 | + '.env.local', |
| 144 | + '.env.[mode]', |
| 145 | + '.env.[mode].local', |
| 146 | + '.env.override', // Always loaded last |
| 147 | + ], |
| 148 | + }, |
| 149 | +}; |
| 150 | +``` |
| 151 | + |
| 152 | +T> The `[mode]` placeholder will be replaced with the current webpack mode. For example, in `production` mode, `.env.[mode]` becomes `.env.production`. |
| 153 | + |
| 154 | +## File Priority |
| 155 | + |
| 156 | +Environment files are loaded in order, with later files having higher priority: |
| 157 | + |
| 158 | +1. `.env` - Loaded in all modes |
| 159 | +2. `.env.local` - Loaded in all modes, ignored by git (convention) |
| 160 | +3. `.env.[mode]` - Only loaded in specified mode (e.g., `.env.production`) |
| 161 | +4. `.env.[mode].local` - Only loaded in specified mode, ignored by git |
| 162 | + |
| 163 | +Variables from later files override those from earlier files. Additionally, variables already set in `process.env` take the highest priority. |
| 164 | + |
| 165 | +## Variable Expansion |
| 166 | + |
| 167 | +Environment variables are automatically expanded using the [dotenv-expand](https://github.com/motdotla/dotenv-expand) syntax: |
| 168 | + |
| 169 | +**.env** |
| 170 | + |
| 171 | +```bash |
| 172 | +WEBPACK_API_BASE=https://api.example.com |
| 173 | +WEBPACK_API_URL=${WEBPACK_API_BASE}/v1 |
| 174 | +WEBPACK_PORT=${WEBPACK_PORT:-3000} # Use WEBPACK_PORT from process.env, or 3000 as default |
| 175 | +``` |
| 176 | + |
| 177 | +**In your code:** |
| 178 | + |
| 179 | +```javascript |
| 180 | +console.log(process.env.WEBPACK_API_URL); // "https://api.example.com/v1" |
| 181 | +console.log(process.env.WEBPACK_PORT); // Value of process.env.WEBPACK_PORT if set, otherwise "3000" |
| 182 | +``` |
| 183 | + |
| 184 | +T> During expansion, you can reference any variable from `process.env` (e.g., `${PORT}`), but only variables with the specified prefix (e.g., `WEBPACK_`) will be exposed in your final bundle. In the example above, `${WEBPACK_PORT:-3000}` references `WEBPACK_PORT` from `process.env` if it exists. |
| 185 | + |
| 186 | +**Expansion behavior example:** |
| 187 | + |
| 188 | +```bash |
| 189 | +# .env file |
| 190 | +WEBPACK_API_URL=${API_BASE:-https://default.com}/api |
| 191 | +``` |
| 192 | + |
| 193 | +```bash |
| 194 | +# Run with environment variable |
| 195 | +API_BASE=https://custom.com npm run build |
| 196 | +``` |
| 197 | + |
| 198 | +Result: `process.env.WEBPACK_API_URL` will be `"https://custom.com/api"` because `API_BASE` from `process.env` is used during expansion, even though `API_BASE` itself won't be exposed in the bundle (it lacks the `WEBPACK_` prefix). |
| 199 | + |
| 200 | +## Usage Examples |
| 201 | + |
| 202 | +### Basic Usage |
| 203 | + |
| 204 | +Create a `.env` file in your project root: |
| 205 | + |
| 206 | +**.env** |
| 207 | + |
| 208 | +```bash |
| 209 | +WEBPACK_API_URL=https://api.example.com |
| 210 | +WEBPACK_FEATURE_FLAG=true |
| 211 | +SECRET_KEY=should-not-be-exposed # Won't be exposed (no WEBPACK_ prefix) |
| 212 | +``` |
| 213 | + |
| 214 | +**webpack.config.js** |
| 215 | + |
| 216 | +```javascript |
| 217 | +module.exports = { |
| 218 | + //... |
| 219 | + dotenv: true, // Uses default prefix "WEBPACK_" |
| 220 | +}; |
| 221 | +``` |
| 222 | + |
| 223 | +**In your application:** |
| 224 | + |
| 225 | +```javascript |
| 226 | +console.log(process.env.WEBPACK_API_URL); // "https://api.example.com" |
| 227 | +console.log(process.env.WEBPACK_FEATURE_FLAG); // "true" |
| 228 | +console.log(process.env.SECRET_KEY); // undefined (not exposed) |
| 229 | +``` |
| 230 | + |
| 231 | +### Mode-Specific Configuration |
| 232 | + |
| 233 | +Create mode-specific files: |
| 234 | + |
| 235 | +**.env** |
| 236 | + |
| 237 | +```bash |
| 238 | +WEBPACK_API_URL=https://api.example.com |
| 239 | +WEBPACK_DEBUG=false |
| 240 | +``` |
| 241 | + |
| 242 | +**.env.production** |
| 243 | + |
| 244 | +```bash |
| 245 | +WEBPACK_API_URL=https://prod-api.example.com |
| 246 | +WEBPACK_DEBUG=false |
| 247 | +``` |
| 248 | + |
| 249 | +**.env.development** |
| 250 | + |
| 251 | +```bash |
| 252 | +WEBPACK_API_URL=https://dev-api.example.com |
| 253 | +WEBPACK_DEBUG=true |
| 254 | +``` |
| 255 | + |
| 256 | +When building with `--mode production`, `WEBPACK_API_URL` will be `"https://prod-api.example.com"`. |
| 257 | + |
| 258 | +### Multiple Prefixes |
| 259 | + |
| 260 | +Expose variables with different prefixes: |
| 261 | + |
| 262 | +**.env** |
| 263 | + |
| 264 | +```bash |
| 265 | +APP_NAME=MyApp |
| 266 | +APP_VERSION=1.0.0 |
| 267 | +CONFIG_TIMEOUT=5000 |
| 268 | +CONFIG_RETRY=3 |
| 269 | +PRIVATE_KEY=secret # Won't be exposed |
| 270 | +``` |
| 271 | + |
| 272 | +**webpack.config.js** |
| 273 | + |
| 274 | +```javascript |
| 275 | +module.exports = { |
| 276 | + //... |
| 277 | + dotenv: { |
| 278 | + prefix: ['APP_', 'CONFIG_'], |
| 279 | + }, |
| 280 | +}; |
| 281 | +``` |
| 282 | + |
| 283 | +### Custom Directory and Template |
| 284 | + |
| 285 | +Load environment files from a custom location with custom naming: |
| 286 | + |
| 287 | +**webpack.config.js** |
| 288 | + |
| 289 | +```javascript |
| 290 | +module.exports = { |
| 291 | + //... |
| 292 | + dotenv: { |
| 293 | + dir: './environments', |
| 294 | + template: ['.env.base', '.env.[mode]'], |
| 295 | + }, |
| 296 | +}; |
| 297 | +``` |
| 298 | + |
| 299 | +This will load: |
| 300 | + |
| 301 | +- `./environments/.env.base` |
| 302 | +- `./environments/.env.production` (in production mode) |
| 303 | + |
| 304 | +## Security Considerations |
| 305 | + |
| 306 | +W> **Never commit sensitive data to version control!** |
| 307 | + |
| 308 | +- Use `.gitignore` to exclude `.env.local` and `.env.[mode].local` files |
| 309 | +- Only expose environment variables with specific prefixes |
| 310 | +- Never use an empty string `''` as a prefix |
| 311 | +- Consider using different `.env` files for different environments |
| 312 | +- Store production secrets in your deployment platform's environment variables |
| 313 | + |
| 314 | +**.gitignore** |
| 315 | + |
| 316 | +```bash |
| 317 | +# local env files |
| 318 | +.env.local |
| 319 | +.env.*.local |
| 320 | +``` |
0 commit comments