Registry / devops / webpack-dotenv-plugin

webpack-dotenv-plugin

JSON →
library2.1.0jsnpmunverified

A webpack plugin that uses dotenv-safe to load environment variables from a .env file and make them available as process.env replacements during the webpack build. Version 2.1.0 requires webpack >=1.13.0 as a peer dependency. It supports custom paths for .env and sample files, respects externally set environment variables, and fixed a deprecation warning for webpack 4.x. Unlike other dotenv-webpack plugins, this one uses dotenv-safe to ensure required variables are defined. Release cadence is irregular; last update was in 2018.

npm install webpack-dotenv-plugin
INSTALL
IMPORT
SIG · WEBPACK-DOTENV-PLU
W
webpack-dotenv-plugin
devopsjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DotenvPlugin
const DotenvPlugin = require('webpack-dotenv-plugin');
import DotenvPlugin from 'webpack-dotenv-plugin';
Package does not ship ESM; CommonJS require is required.
DotenvPlugin
const DotenvPlugin = require('webpack-dotenv-plugin');
This is the default export; no named exports exist.
DotenvPlugin
import DotenvPlugin from 'webpack-dotenv-plugin';
const { DotenvPlugin } = require('webpack-dotenv-plugin');
If using ES module syntax with bundler, default import works, but named import is incorrect.

Configures webpack to load environment variables from .env file using dotenv-safe, with sample file and allowEmptyValues option.

// webpack.config.js const DotenvPlugin = require('webpack-dotenv-plugin'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: __dirname + '/dist' }, plugins: [ new DotenvPlugin({ sample: './.env.sample', path: './.env', allowEmptyValues: true }) ] };
Debug
Known issues
breakingIn v2.0.0, the sample file path changed from '.env.default' to '.env.sample'. If you have a .env.default file, it will not be found.
fix
Rename .env.default to .env.sample or update the sample option to point to your existing file.
affects: >=2.0.0
breakingIn v2.0.0, webpack was moved to a peer dependency. You must install webpack separately.
fix
Install webpack: npm install --save-dev webpack
affects: >=2.0.0
deprecatedv2.1.0 uses dotenv-safe 5.0.1, which may have deprecations. Check dotenv-safe changelog.
fix
Review dotenv-safe 5.0.1 release notes for any deprecated options.
affects: 2.1.0
gotchaThe plugin stringifies environment variables, so all values become strings. Boolean or numeric values in .env will be converted to strings.
fix
Parse values in your code as needed (e.g., Boolean(process.env.DEBUG)).
affects: *
gotchaExternally set environment variables override those in .env. This may cause unexpected behavior if your system has conflicting variables.
fix
Be aware of the override behavior; unset external variables if you want .env to take precedence.
affects: *
Errors
Common errors & fixes
Module not found: Error: Can't resolve 'webpack-dotenv-plugin'
Package not installed or installed as devDependency but not in node_modules.
fix
Run: npm install --save-dev webpack-dotenv-plugin
Error: Cannot find module 'dotenv-safe'
dotenv-safe is a transitive dependency, but may not be resolved if peer deps are missing.
fix
Ensure webpack is installed: npm install --save-dev webpack
DeprecationWarning: Tapable.apply is deprecated
Using plugin with webpack 4+; older plugin versions had this warning.
fix
Upgrade to webpack-dotenv-plugin >=2.1.0
no such file or directory, open '.env.sample'
The sample file path default was changed in v2.0.0 from .env.default to .env.sample.
fix
Create a .env.sample file or specify sample option to point to your existing file.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
webpackrequiredPeer dependency; required to function as a webpack plugin.
Agent activity
11 hits · last 30 days
node
10
Resources
webpack-dotenv-plugin — npm install webpack-dotenv-plugin · libregistry