Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP_DEPLOYMENT_ENV undefined in production build #18

Open
foxlau opened this issue Dec 6, 2024 · 1 comment
Open

APP_DEPLOYMENT_ENV undefined in production build #18

foxlau opened this issue Dec 6, 2024 · 1 comment

Comments

@foxlau
Copy link

foxlau commented Dec 6, 2024

Description

Environment variable APP_DEPLOYMENT_ENV is undefined when running the production build (npm run build && npm start), but works fine in development mode (npm run dev).

Steps to Reproduce

  1. .env file exists with APP_DEPLOYMENT_ENV="production"
  2. npm run dev - works fine
  3. npm run build && npm start - fails with:
DEBUG: Raw process.env: { NODE_ENV: 'production', APP_DEPLOYMENT_ENV: undefined }
❌ Invalid environment variables: { APP_DEPLOYMENT_ENV: [ 'Required' ] }

Environment

  • Node.js: v22.12.0
  • npm: v10.9.0
  • pnpm: v9.14.2
  • Works in dev mode (npm run dev)
  • Fails only in production build

Could this be related to how environment variables are handled during the production build process?

@thomasfr
Copy link
Contributor

Hi @foxlau thanks for creating the issue.
The build does not include the env vars which are set during the build process (running pnpm run build). When you run your application with pnpm run start it tries to read those environment variables from process.env, if they are not set, it fails with the above error.
During development (pnpm run dev), you dont see this error cause the react-router dev server is automatically trying to load an .env file to load them as environment variables. In production though, they should be set as actual environment variables.
If you want to test that out locally you can try to do:

pnpm run build
APP_DEPLOYMENT_ENV=production pnpm run start

or if you have dotenvx installed globally, you can run this to let dotenvx try to load env vars from an .env file like so:

pnpm run build
dotenvx run -- pnpm run start

On a production server/deployment you should set the environment variables according to the documentation of your chosen deployment platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants