How to Set Environment Variables for Different Apps in a Turbo Monorepo

15077693dHOBBY

3 months ago

Hello Railway community,

I'm working on a project using a Turbo monorepo setup, and I'm trying to figure out the best way to set environment variables for different apps within the monorepo. Here's a brief overview of my setup:

File Structure

- Root Directory

- turbo.json

- apps/

- web/

- .env

- api/

- .env

- packages/

- database/

- .env

How can I set environment variables specifically for each app within the monorepo? For example, I want VITE_API_URL to be different for the web app and DATABASE_URL to be specific to the api app.

Any guidance or examples on how to achieve this would be greatly appreciated!

Thank you!

Solved

27 Replies


3 months ago

Hello,

You would deploy the multiple apps as multiple Railway services, and then you can set app-specific service variables on each service.

You would use turbo's filter flag to selectively build and start each app, and you would set those commands in the service settings for the respective service.


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

But I'm using shared package so my package have .env, the app have .env
So I'm asking how to set .env inside the folder.


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

Yep what I've said still applies to shared monorepos, you want a different Railway service for each package.


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

why apps/api or apps/web cannot get the environment variables I set up from railway?


Status changed to Awaiting Railway Response railway[bot] 3 months ago



3 months ago

Hello,

You still only have one service deployed.

You have two packages, you need two Railway services.

Get the apps deployed correctly and we can continue debugging from there.


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

I open two services now. Please let me know how to access the environment variable from railway. Thanks


Status changed to Awaiting Railway Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

bfad04ed-af43-4cdd-b998-ae0dd58229


brody

Hello,You would deploy the multiple apps as multiple Railway services, and then you can set app-specific service variables on each service.You would use turbo's filter flag to selectively build and start each app, and you would set those commands in the service settings for the respective service.

3 months ago

Please see my previous advice, you currently have each service running the same 3 packages.


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

I have one package(database) and two app (api and web)
Have many service should I open?
You want me open three service (database, api and web),
if yes how do i use the database from api or web?


Status changed to Awaiting Railway Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

Do you advice me open two service?
1. for web: build web and database package

  1. for api: build api and database package


15077693dHOBBY

3 months ago

If yes how to do it?
Do I need to change my turbo.json?


3 months ago

Your database package just handles the database logic, it is not a web server thus it does not need a Railway service.

Please see my first message of this thread.


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

My web is using Database package types.


Status changed to Awaiting Railway Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

So Do you suggest I set the root is apps/web and apps/api?
And ingore the database package?


3 months ago

Nope, you have a shared monorepo, you do not want to set a root directory.


Status changed to Awaiting User Response railway[bot] 3 months ago


brody

Hello,You would deploy the multiple apps as multiple Railway services, and then you can set app-specific service variables on each service.You would use turbo's filter flag to selectively build and start each app, and you would set those commands in the service settings for the respective service.

3 months ago

You would use turbo's filter flag to selectively build and start each app, and you would set those commands in the service settings for the respective service.


15077693dHOBBY

3 months ago

Thanks for your advice I split two service now.
How to access the environment variable from railway?


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

Perfect, now you have each service starting the package its supposed to!

As for making sure the environment variables are available, please see turbo's docs -

https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables#loose-mode


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

I can access the .env in local run build but not in railway.
May I have your help please?

Attachments


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

Please read the Turbo documentation I have linked in my previous message.


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

I read it and I followed the guide from the doc. It run properly in local but not railway.


Status changed to Awaiting Railway Response railway[bot] 3 months ago



3 months ago

Please read the Turbo documentation I have linked in my previous message.


Status changed to Awaiting User Response railway[bot] 3 months ago


15077693dHOBBY

3 months ago

https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables#best-practices
According the doc, I put the env inside paackage and app.
How to config env in each package and app?


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

I'm sorry but we cannot provide support for turbo, you would want to reference the documentation I have linked.


Status changed to Awaiting User Response railway[bot] 3 months ago


How to Set Environment Variables for Different Apps in a Turbo Monorepo - Railway Help Station