firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

NextJS environment variables inaccessible in server-side code when deployed with Web Frameworks

Open aquaticone opened this issue 4 years ago • 9 comments

When deploying a NextJS application to Firebase using the Web Frameworks beta, environment variables defined in .env files are not available in server-side code.

[REQUIRED] Environment info

firebase-tools: 11.16.0

Platform: macOS

[REQUIRED] Test case

Deployment: https://web-frameworks-test.web.app/

Repo: https://github.com/aquaticone/web-frameworks-test

[REQUIRED] Steps to reproduce

  1. Create Next app with npx create-next-app@latest
  2. Enable Web Frameworks beta: firebase experiments:enable webframeworks
  3. Firebase init: firebase init
  4. Create .env file with server-only variables; use in server-side code
  5. Deploy to Firebase

[REQUIRED] Expected behavior

Environment variables defined in .env are accessible in NextJS API routes and server-side functions.

[REQUIRED] Actual behavior

Environment variables defined in .env are not accessible when deployed to Firebase.

aquaticone avatar Nov 16 '22 08:11 aquaticone

I'd imagine this is "expected behaviour" as firebase relies heavily on their config store.

Hacksore avatar Nov 17 '22 02:11 Hacksore

We are facing the same issue in our project. We tried initializing firebase-functions and copied the .env within functions just as an experiment to see if that would work. However, that too did not work out.

It would be great if someone can take a look at this or direct us in the right direction so we can fix it ourselves.

Thanks.

hassaanp avatar Nov 28 '22 11:11 hassaanp

+1 looking for any documentation/direction on how to feed private .env variables into firebase-deployed nextjs api routes

Edit: I was able to get my server routes running with all my secret vars by running firebase functions:secrets:set <VAR_NAME> one by one, and then configuring access to them via the Google Cloud console

benjaminben avatar Dec 10 '22 19:12 benjaminben

+1 looking for any documentation/direction on how to feed private .env variables into firebase-deployed nextjs api routes

Edit: I was able to get my server routes running with all my secret vars by running firebase functions:secrets:set <VAR_NAME> one by one, and then configuring access to them via the Google Cloud console

By doing this would you simply access the secrets via the process.env? I'm also having the same issue and it's bugging me.

mrjackyliang avatar Jan 08 '23 00:01 mrjackyliang

@mrjackyliang yes, once you expose the variables to your ssr cloud function via the Secret Manager and the Cloud Functions console, they'll be available under process.env as normal

benjaminben avatar Jan 08 '23 00:01 benjaminben

@mrjackyliang yes, once you expose the variables to your ssr cloud function via the Secret Manager and the Cloud Functions console, they'll be available under process.env as normal

Confirming it works! The secrets also are preserved when you publish a new version. Nice and thanks!

mrjackyliang avatar Jan 08 '23 00:01 mrjackyliang

@benjaminben I also ran into this; I created https://github.com/firebase/firebase-tools/issues/5514 to bring this up as it would be important that this would work automatically, feel free to add anything there.

steebchen avatar Feb 14 '23 08:02 steebchen

@benjaminben how did you setup your CI/CD for this matter

markoboras0712 avatar Jul 01 '24 16:07 markoboras0712

@benjaminben how did you setup your CI/CD for this matter

To be honest this was far back enough that I don't even remember which project I set it up on 😅 I believe that as mrjackyliang commented above the secrets are preserved when you publish updates to your NextJS app, but if you want to make changes to your env variables you need to run firebase functions:secrets:set <VAR_NAME> one by one (and if you want to add new ones you'll need the extra step of configuring access to the new variable in GCP after setting them via firebase CLI). At least that was the case back back in early 2023, I've since switched away from firebase hosting for NextJS although it appears this feature is still in "early public preview"

benjaminben avatar Jul 01 '24 17:07 benjaminben