react/create-react-app · critical · Error

The NODE_ENV environment variable is required but was not sp

Error message

The NODE_ENV environment variable is required but was not specified.

What it means

react-scripts/config/env.js loads dotenv files keyed off NODE_ENV. Because the file list (`.env.${NODE_ENV}.local`, etc.) depends on it, the script refuses to proceed when NODE_ENV is falsy rather than silently loading the wrong files. This runs at the very top of every react-scripts script.

Source

Thrown at packages/react-scripts/config/env.js:20

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
// @remove-on-eject-end
'use strict';

const fs = require('fs');
const path = require('path');
const paths = require('./paths');

// Make sure that including paths.js after env.js will read .env variables.
delete require.cache[require.resolve('./paths')];

const NODE_ENV = process.env.NODE_ENV;
if (!NODE_ENV) {
  throw new Error(
    'The NODE_ENV environment variable is required but was not specified.'
  );
}

// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFiles = [
  `${paths.dotenv}.${NODE_ENV}.local`,
  // Don't include `.env.local` for `test` environment
  // since normally you expect tests to produce the same
  // results for everyone
  NODE_ENV !== 'test' && `${paths.dotenv}.local`,
  `${paths.dotenv}.${NODE_ENV}`,
  paths.dotenv,
].filter(Boolean);

// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set.  Variable expansion is supported in .env files.

View on GitHub (pinned to 6254386531)

Solutions

  1. Always invoke react-scripts through its package.json scripts (`react-scripts start/build/test`), which set NODE_ENV appropriately.
  2. If calling config/env.js from custom code, set process.env.NODE_ENV before requiring it.
  3. Export NODE_ENV in your shell/CI before the build: `NODE_ENV=production`.
  4. For ejected setups, replicate react-scripts' NODE_ENV assignment at the top of your entry file.

Example fix

// before
// custom script
require('./config/env');
// after
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
require('./config/env');
Defensive patterns

Strategy: validation

Validate before calling

// Set a default before requiring config/env.js
function ensureNodeEnv(defaultEnv = 'development') {
  if (!process.env.NODE_ENV) {
    process.env.NODE_ENV = defaultEnv;
  }
}
ensureNodeEnv();
require('./config/env');

Type guard

const hasNodeEnv = () =>
  ['development', 'test', 'production'].includes(process.env.NODE_ENV);

Try / catch

try {
  require('react-scripts/config/env');
} catch (e) {
  if (/NODE_ENV environment variable is required/.test(e.message)) {
    console.error('Set NODE_ENV before running react-scripts (e.g. NODE_ENV=production).');
    process.exit(1);
  }
  throw e;
}

Prevention

When it happens

Trigger: Importing config/env.js (directly or via any react-scripts script) when process.env.NODE_ENV is undefined or empty. The `if (!NODE_ENV)` guard fires immediately.

Common situations: Running a react-scripts script (start/build/test) from a shell or runner that doesn't set NODE_ENV. Ejected projects where a custom entry imports env.js before setting NODE_ENV. CI containers that clear the environment. Node versions/configs that don't default NODE_ENV.

Related errors


AI-assisted analysis of react/create-react-app@6254386531 (2026-08-12). Data as JSON: /api/errors/fc5bdbd8f07c3852. Report an issue: GitHub.