Issue regarding environment variables
monnss69
FREEOP

5 months ago

So when I use godotenv.Load() in my production code, all the env variables will not be found even though I have set the variable in Railway and deploy it. However it works when I remove the godotenv.Load().

The problem is:
I need to use godotenv.Load() in development, how can i resolve this?

Solved$10 Bounty

10 Replies

monnss69
FREEOP

5 months ago

bump


noahd
EMPLOYEE

5 months ago

It looks like (according to google)
godotenv.Load() overwrites existing env vars possibly replacing the ones injected by railway. If you were to remove that from your code for testing purposes would it work?


monnss69
FREEOP

5 months ago

If I remove it from the code, the deployment works. However, the problem it there are too much hassle with it since it is needed in development stage, and I have to remove it in deployments.


monnss69
FREEOP

5 months ago

bum


monnss69

If I remove it from the code, the deployment works. However, the problem it there are too much hassle with it since it is needed in development stage, and I have to remove it in deployments.

noahd
EMPLOYEE

5 months ago

Can you explain a bit more about this? I'm not quite sure I follow.
Your .env injects env variables into runtime in dev.
In production railway does this so you can read them just fine.


monnss69
FREEOP

5 months ago

deleted


noahd

Can you explain a bit more about this? I'm not quite sure I follow. Your .env injects env variables into runtime in dev. In production railway does this so you can read them just fine.

monnss69
FREEOP

5 months ago

Yes, as usual you don't push your .env on production code right.

So, now when I deploy my production code, I suspect the godotenv.Load() will not find my .env and overwrites all my variables that I set on Railway and therefore the deployments on Railway can't find my environment variable.

That's why atm, before pushing code to production I have to remove all the godotenv.Load() statement used in my development code.


monnss69

Yes, as usual you don't push your .env on production code right.So, now when I deploy my production code, I suspect the godotenv.Load() will not find my .env and overwrites all my variables that I set on Railway and therefore the deployments on Railway can't find my environment variable.That's why atm, before pushing code to production I have to remove all the godotenv.Load() statement used in my development code.

noahd
EMPLOYEE

5 months ago

If you can detect whether you are in production with an env flag you can conditionally do godotenv.Load().
In prod use the variables section.
What I do is on start have something that exports all the env vars I need into a config file (config.rs or config.go) and that config.go will cause a crash if it cant find all the variables it needs ensuring everything is started and you wont crash from no variables at runtime


noahd

If you can detect whether you are in production with an env flag you can conditionally do godotenv.Load().In prod use the variables section. What I do is on start have something that exports all the env vars I need into a config file (config.rs or config.go) and that config.go will cause a crash if it cant find all the variables it needs ensuring everything is started and you wont crash from no variables at runtime

monnss69
FREEOP

5 months ago

That sounds good, I will try it.


monnss69

That sounds good, I will try it.

noahd
EMPLOYEE

5 months ago

Sounds good! Happy I could help


Status changed to Solved brody 5 months ago


Loading...