Environment variables are undefined during runtime Turborepo
pedroota
HOBBYOP

a year ago

Even declaring the variables on turbo.json, on Railway still undefined

That's my turbo.json on the /apps/server directory:

{
  "extends": ["//"],
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "inputs": [
        "$TURBO_DEFAULT$",
        ".env.production.local",
        ".env.local",
        ".env.production",
        ".env"
      ],
      "outputs": ["dist/**", ".next/**", "!.next/cache/**"],
      "env": ["DATABASE_URL", "NODE_ENV", "PORT"]
    },
    "dev": {
      "cache": false,
      "persistent": true,
      "inputs": [
        "$TURBO_DEFAULT$",
        ".env.production.local",
        ".env.local",
        ".env.production",
        ".env"
      ],
      "env": ["DATABASE_URL", "NODE_ENV", "PORT"]
    }
  }
}
Solved$10 Bounty

16 Replies

pedroota
HOBBYOP

a year ago

6452898b-4334-4a3f-b118-d8bfb71e8f8c


supercool27
FREE

a year ago

  • Declaring env in turbo.json does not assign values — it only defines which variables should be passed if they exist.
  • Go to Railway Dashboard > Variables section and manually add your environment variables, for example:
    • DATABASE_URL=your_value
    • NODE_ENV=production
    • PORT=8000
  • If you're using .env files, make sure to load them manually in your entry file using:
js  

CopyEdit

require('dotenv').config();

  • Now process.env.DATABASE_URL and others will work correctly at build and runtime.

a year ago

You could try enabling Turborepo's loose mode.


pedroota
HOBBYOP

a year ago

I did that, and it solved. But their docs on the strict mode (the recommend way) is very bad imo. Do you know how to properly handle that?


a year ago

I unfortunately am not familiar with Turborepo, perhaps you can try asking in the Vercel forum with the Turborepo tag.


a year ago

sorry to volunteer you @ThallesComH but you are a turborepo expert


a year ago

If you're specifying all the environment variables in turbo.json, then it should just work.

Also, it seems like you're creating a turbo.json per app/workspace, which may be causing that issue, and as far as I know, that's incorrect. The turbo.json file should be located at the root directory only.


pedroota
HOBBYOP

a year ago

Should I specify all the environment variables of all the workspaces on the “env” property inside the build step then?


pedroota
HOBBYOP

a year ago

On the root turbo.json ofc


a year ago

yep, if you want there's globalEnv at root to set for all steps too.


a year ago

btw, if you're using a Dockerfile and attempting to access environment variables at build time, be aware that you'll need to modify your Dockerfile a bit.


pedroota
HOBBYOP

a year ago

Hmm, what do you mean?


a year ago

If you're accessing environment variables at build time (such as process.env.) and you're using a Dockerfile or any tool that requires those environment variables during build time, Railway requires you to modify your code slightly. This isn't related to Turborepo, but I wanted to ensure you're not affected by this circumstance.


a year ago

for example running a database migration at build time where it needs the DATABASE_URL at that stage.


a year ago

Hey @pedro, do you still need help with setting up your turbo.json for envs?


a year ago

!s


Status changed to Solved uxuz 11 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...