NextJS environment variables inaccessible in server-side code when deployed with Web Frameworks
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
- Create Next app with
npx create-next-app@latest - Enable Web Frameworks beta:
firebase experiments:enable webframeworks - Firebase init:
firebase init - Create
.envfile with server-only variables; use in server-side code - 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.
I'd imagine this is "expected behaviour" as firebase relies heavily on their config store.
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.
+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
+1 looking for any documentation/direction on how to feed private
.envvariables into firebase-deployed nextjs api routesEdit: 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 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
@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!
@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.
@benjaminben how did you setup your CI/CD for this matter
@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"