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

Environment Variable Loading #226

Closed
beerose opened this issue Oct 13, 2021 · 1 comment · Fixed by blitz-js/blitz#2878
Closed

Environment Variable Loading #226

beerose opened this issue Oct 13, 2021 · 1 comment · Fixed by blitz-js/blitz#2878

Comments

@beerose
Copy link
Contributor

beerose commented Oct 13, 2021

Discussed in blitz-js/blitz#2444

Originally posted by flybayer June 3, 2021

Problem

Blitz/Next use basically dotenv-flow to load env var files like .env, .env.test, .env.development.local, etc.

That behavior relies on setting NODE_ENV to the appropriate value before invoking a CLI command.

Problems:

  1. Nextjs internals don't allow changing NODE_ENV. The build command must use NODE_ENV=production and dev must use NODE_ENV=development.
  2. There's a new Nextjs bug that breaks previous usage of NODE_ENV=test blitz build
  3. Now it's impossible to use NODE_ENV inside your app code to conditionally do things based on different environments.

Proposed Solution

Use APP_ENV to determine app environment instead of NODE_ENV. NODE_ENV will only be used for blitz dev vs blitz build.

The value of APP_ENV will determine which .env.[ENV].* files will be loaded.

  1. Defaults:
    1. APP_ENV=development for blitz dev
    2. APP_ENV=production for blitz build and blitz start
    3. APP_ENV=test for Jest tests
  2. Ways to manually specify APP_ENV:
    1. Set it before invoking the CLI like APP_ENV=test blitz start
    2. Set it with a new -e flag like blitz start -e test

With this solution, you can set APP_ENV to any string. Example: APP_ENV=staging which would load .env.staging


Any and all feedback is welcome!

@fleck
Copy link

fleck commented Nov 24, 2021

Nice, just spent the last hour trying to figure out why NODE_ENV was getting set to production when I thought it should be test!

@itsdillon itsdillon transferred this issue from blitz-js/blitz Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants