How to Access ENV Variables in Google Cloud Run using Next.js

Viewed 341

I am using Nextjs, I am deploying my application on cloud run.

I use the command process.env.VARIABLE_NAME within my localhost and it works great, I make sure that the .env is in git ignore so that it doesnt get uploaded.

However, I then deploy my application using the google cloud run... I make sure I go to the Enviroments tab and add in exactly the same variables that are within the .env file. but can't get environment variables through process.env

1 Answers

I had the same issue trying to access my NEXT_PUBLIC envs in Cloud Run, so while i was searching I've found 3 possible solutions:

  1. using getServerSideProps to get the env variables and put in the client, this worked because in my case the envs were appearing in the server, just in the client that i couldn't get it.
  2. declare the ENV in the dockerFile and pass it during the build like Consume Cloud Run environment variables inside Nextjs app suggests
  3. using an external library like react-env as suggested in: NextJS - How to get runtime environment variables
Related