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

2

8

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

Gui answered 15/11, 2021 at 9:48 Comment(1)
Does this answer your question? Environment variables are undefined during Cloud Run BuildBivens
N
6

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
Neaten answered 1/7, 2022 at 15:47 Comment(0)
F
0

I also had the same issue with nextjs but i was deploying it to vercel and my serverless function was detecting the enviroment variables in localhost but when I was deploying it to vercel the enviroment variables were not detected.

And I found out that if we had enviroment variables defined in your local enviroment that our serverless function depend on then we have to also declared it inside enviroment variable page of our Project settings.

Here is the Docs for defining enviroment variable: Enviroment Variables

Here are some other reason why nextjs serverless function works on localhost and might now work when deployed: Why does my Serverless Function work locally but not when deployed?

Frederique answered 4/11, 2022 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.