|
| 1 | +# Dotenv Webpack Plugin |
| 2 | + |
| 3 | +`DotenvWebpackPlugin` is a webpack plugin to allow consumers to load environment variables from `.env` files. It does a text replace in the resulting bundle for any instances of `process.env` and `import.meta.env`. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install dotenv-webpack-plugin --save-dev |
| 9 | +``` |
| 10 | + |
| 11 | +```bash |
| 12 | +yarn add dotenv-webpack-plugin --dev |
| 13 | +``` |
| 14 | + |
| 15 | +```bash |
| 16 | +pnpm add dotenv-webpack-plugin -D |
| 17 | +``` |
| 18 | + |
| 19 | +## Basic Usage |
| 20 | + |
| 21 | +`DotenvWebpackPlugin` exposes env variables under `process.env` and `import.meta.env` objects as strings automatically. |
| 22 | + |
| 23 | +To prevent accidentally leaking env variables to the client, only variables prefixed with `WEBPACK_` are exposed to Webpack-bundled code. e.g. for the following `.env` file: |
| 24 | + |
| 25 | +```bash |
| 26 | +WEBPACK_SOME_KEY=1234567890 |
| 27 | +SECRET_KEY=abcdefg |
| 28 | +``` |
| 29 | + |
| 30 | +Only `WEBPACK_SOME_KEY` is exposed to Webpack-bundled code as `import.meta.env.WEBPACK_SOME_KEY` and `process.env.WEBPACK_SOME_KEY`, but `SECRET_KEY` is not. |
| 31 | + |
| 32 | +```javascript |
| 33 | +const DotenvWebpackPlugin = require("dotenv-webpack-plugin"); |
| 34 | + |
| 35 | +module.exports = { |
| 36 | + // Existing configuration options... |
| 37 | + plugins: [new DotenvWebpackPlugin()], |
| 38 | +}; |
| 39 | +``` |
| 40 | + |
| 41 | +## `.env` Files |
| 42 | + |
| 43 | +Environment variables are loaded from the following files in your [environment directory](): |
| 44 | + |
| 45 | +``` |
| 46 | +.env # loaded in all cases |
| 47 | +.env.local # loaded in all cases, ignored by git |
| 48 | +.env.[mode] # only loaded in specified mode |
| 49 | +.env.[mode].local # only loaded in specified mode, ignored by git |
| 50 | +``` |
| 51 | + |
| 52 | +> Mode-specific env variables (e.g., `.env.production`) will override conflicting variables from generic environment files (e.g., `.env`). Variables that are only defined in `.env` or `.env.local` will remain available to the client. |
| 53 | +
|
| 54 | +## Using a configuration |
| 55 | + |
| 56 | +You can pass a configuration object to `dotenv-webpack-plugin` to customize its behavior. |
| 57 | + |
| 58 | +### `dir` |
| 59 | + |
| 60 | +Type: |
| 61 | + |
| 62 | +```ts |
| 63 | +type dir = string; |
| 64 | +``` |
| 65 | + |
| 66 | +Default: `""` |
| 67 | + |
| 68 | +Specifies the directory where the plugin should look for environment files. By default, it looks in the root directory. |
| 69 | + |
| 70 | +```js |
| 71 | +new DotenvWebpackPlugin({ |
| 72 | + dir: "./config/env", |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +### `prefix` |
| 77 | + |
| 78 | +Type: |
| 79 | + |
| 80 | +```ts |
| 81 | +type prefix = string | string[]; |
| 82 | +``` |
| 83 | + |
| 84 | +Default: `undefined` |
| 85 | + |
| 86 | +Defines which environment variables should be exposed to the client code based on their prefix. This is a critical security feature to prevent sensitive information from being exposed. |
| 87 | + |
| 88 | +```js |
| 89 | +// Single prefix |
| 90 | +new DotenvWebpackPlugin({ |
| 91 | + prefix: "PUBLIC_", |
| 92 | +}); |
| 93 | + |
| 94 | +// Multiple prefixes |
| 95 | +new DotenvWebpackPlugin({ |
| 96 | + prefix: ["PUBLIC_", "SHARED_"], |
| 97 | +}); |
| 98 | +``` |
0 commit comments