Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Values from .env never reach the bundle with dotenv-webpack

Your error

process.env.API_URL is undefined in the browser even though .env defines it3 more lines it can show

Fix it fast

Most common cause, 1 of 3

The code destructures process.env or reads a computed key

Write each variable as a full member expression.

Fix
// not replaced: const { API_URL } = process.env;
// not replaced: process.env[`${service}_URL`]

// src/config.js
export const config = {
  apiUrl: process.env.API_URL,
  mapsKey: process.env.MAPS_KEY,
};
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

With dotenv-webpack 9 on webpack 5, process.env.KEY stays undefined in three cases: the code destructures process.env or reads a computed key, the build cannot read the .env file and warns Failed to load ./.env., or the value is set only in the CI environment. Spell each key out, give path an absolute file, or turn on systemvars.

  • Applies to webpack 5.113, dotenv-webpack 9.0, Node.js 22, 24 and 26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run one build and look for the plugin's load warning, then search the source for process.env read in any form other than process.env.NAME. A warning points to cause 2, a hit in the search to cause 1, and neither of the two, with the value coming from CI settings, to cause 3.

Run this first
npx webpack 2>&1 | grep -i "failed to load"
grep -rnE "\{[^}]*\}\s*=\s*process\.env|process\.env\[" src/

Then find the line you see in this list and open its fix:

  1. 1The code destructures process.env or reads a computed keyThe grep finds const { ... } = process.env or process.env[name], and logging process.env in the browser prints MISSING_ENV_VAR
  2. 2The .env file is not where the plugin reads itThe build output contains Failed to load ./.env. (unless silent: true hides it)
  3. 3The value lives in the CI or shell environment, not in .envLocal builds with a .env file are fine; a pipeline or hosting build that sets the variable in its settings ships undefined

What does the error look like?

  • process.env.API_URL is undefined in the browser even though .env defines it
  • console.log(process.env) prints MISSING_ENV_VAR
  • Failed to load ./.env.
  • The variable works on my machine but is undefined in the CI or hosting build

Cause 1 of 3

The code destructures process.env or reads a computed key

You seeThe grep finds const { ... } = process.env or process.env[name], and logging process.env in the browser prints MISSING_ENV_VAR

Check

dotenv-webpack leaves process.env.API_URL undefined when the source never contains that exact text. The plugin gives DefinePlugin one entry per variable, named process.env.NAME, and its README states that destructuring is not supported. On a webpack 5 browser target it then swaps any leftover bare process.env for the string MISSING_ENV_VAR, so a destructured field comes out undefined.

Check
grep -rnE "process\.env(\[|\s*[;,)])" src/

Fix

Write each variable as a full member expression. When you want a settings object, build it from those literal reads in one module and import that module elsewhere.

Fix
// not replaced: const { API_URL } = process.env;
// not replaced: process.env[`${service}_URL`]

// src/config.js
export const config = {
  apiUrl: process.env.API_URL,
  mapsKey: process.env.MAPS_KEY,
};

Verify

Rebuild; the real value now appears in the emitted JavaScript instead of the variable name.

Verify
npx webpack && grep -rn "api.example.com" dist/ | head -3

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The .env file is not where the plugin reads it

You seeThe build output contains Failed to load ./.env. (unless silent: true hides it)

Check

Failed to load ./.env. means dotenv-webpack could not open the file, so no value comes from it. The default path is ./.env, and Node.js resolves a relative path against the current working directory, not against webpack.config.js. Starting webpack from a monorepo root or a parent folder breaks it, and so does keeping the values in a file with another name, such as .env.local.

Check
pwd
ls -a | grep "^\.env"
grep -n -A2 "new Dotenv" webpack.config.js

Fix

Pass the plugin a full path that starts from the folder holding webpack.config.js, and use the real file name.

Fix
const path = require('path');
const Dotenv = require('dotenv-webpack');

module.exports = {
  plugins: [
    new Dotenv({ path: path.resolve(__dirname, '.env.local') }),
  ],
};

Verify

The warning is gone however you start the build. grep -c should print 0.

Verify
npx webpack 2>&1 | grep -c "Failed to load"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The value lives in the CI or shell environment, not in .env

You seeLocal builds with a .env file are fine; a pipeline or hosting build that sets the variable in its settings ships undefined

Check

dotenv-webpack ignores variables of the shell that runs the build unless systemvars is true. The README lists the option as false by default and names CI as the use for it. With it on, the plugin copies the build process environment too, and a system value beats the same key from .env.

Check
echo "API_URL=$API_URL"
grep -n "systemvars" webpack.config.js

Fix

Turn on systemvars. In a pipeline that has no .env file at all, silent: true keeps the expected load warning out of the log.

Fix
new Dotenv({
  systemvars: true,
  silent: process.env.CI === 'true',
})

Verify

Build with the variable set only on the command line and find the value in the output.

Verify
API_URL=https://api.example.com npx webpack && grep -rn "api.example.com" dist/ | head -3

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Commit a .env.example listing every key and set safe: true, so the plugin checks that each listed key has a value.
  • Keep all process.env.NAME reads in one config module; a code review that sees process.env anywhere else asks why.
  • Never commit .env itself: the README warns it can hold secrets, and every value the bundle uses is readable by any visitor.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.