{"record":{"id":"fc5bdbd8f07c3852","repo":"react/create-react-app","slug":"the-node-env-environment-variable-is-required-but","errorCode":null,"errorMessage":"The NODE_ENV environment variable is required but was not specified.","messagePattern":"The NODE_ENV environment variable is required but was not specified\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/react-scripts/config/env.js","lineNumber":20,"sourceCode":"/**\n * Copyright (c) 2015-present, Facebook, Inc.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n// @remove-on-eject-end\n'use strict';\n\nconst fs = require('fs');\nconst path = require('path');\nconst paths = require('./paths');\n\n// Make sure that including paths.js after env.js will read .env variables.\ndelete require.cache[require.resolve('./paths')];\n\nconst NODE_ENV = process.env.NODE_ENV;\nif (!NODE_ENV) {\n  throw new Error(\n    'The NODE_ENV environment variable is required but was not specified.'\n  );\n}\n\n// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use\nconst dotenvFiles = [\n  `${paths.dotenv}.${NODE_ENV}.local`,\n  // Don't include `.env.local` for `test` environment\n  // since normally you expect tests to produce the same\n  // results for everyone\n  NODE_ENV !== 'test' && `${paths.dotenv}.local`,\n  `${paths.dotenv}.${NODE_ENV}`,\n  paths.dotenv,\n].filter(Boolean);\n\n// Load environment variables from .env* files. Suppress warnings using silent\n// if this file is missing. dotenv will never modify any environment variables\n// that have already been set.  Variable expansion is supported in .env files.","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/react/create-react-app/blob/6254386531d263688ccfa542d0e628fbc0de0b28/packages/react-scripts/config/env.js#L2-L38","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Always invoke react-scripts through its package.json scripts (`react-scripts start/build/test`), which set NODE_ENV appropriately.","If calling config/env.js from custom code, set process.env.NODE_ENV before requiring it.","Export NODE_ENV in your shell/CI before the build: `NODE_ENV=production`.","For ejected setups, replicate react-scripts' NODE_ENV assignment at the top of your entry file."],"exampleFix":"// before\n// custom script\nrequire('./config/env');\n// after\nprocess.env.NODE_ENV = process.env.NODE_ENV || 'development';\nrequire('./config/env');","handlingStrategy":"validation","validationCode":"// Set a default before requiring config/env.js\nfunction ensureNodeEnv(defaultEnv = 'development') {\n  if (!process.env.NODE_ENV) {\n    process.env.NODE_ENV = defaultEnv;\n  }\n}\nensureNodeEnv();\nrequire('./config/env');","typeGuard":"const hasNodeEnv = () =>\n  ['development', 'test', 'production'].includes(process.env.NODE_ENV);","tryCatchPattern":"try {\n  require('react-scripts/config/env');\n} catch (e) {\n  if (/NODE_ENV environment variable is required/.test(e.message)) {\n    console.error('Set NODE_ENV before running react-scripts (e.g. NODE_ENV=production).');\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Always invoke builds through react-scripts' own scripts, which set NODE_ENV.","In custom entries, set process.env.NODE_ENV before requiring any react-scripts config.","Export NODE_ENV in CI job definitions.","Add an early startup guard that fails fast with a clear message."],"tags":["environment","node-env","react-scripts","dotenv","config"],"backgroundTag":null,"analyzedSha":"6254386531d263688ccfa542d0e628fbc0de0b28","analyzedAt":"2026-08-12T22:55:01.751Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}