Environment variables are undefined during runtime Turborepo

pedroota
HOBBY

a month 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

1 Replies

pedroota
HOBBY

a month ago

6452898b-4334-4a3f-b118-d8bfb71e8f8c


supercool27
FREE

a month 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:

    CopyEdit

    require('dotenv').config();

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


a month ago

You could try enabling Turborepo's loose mode.


pedroota
HOBBY

a month 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 month ago

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


a month ago

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


a month 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
HOBBY

a month ago

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


pedroota
HOBBY

a month ago

On the root turbo.json ofc


a month ago

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


a month 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
HOBBY

a month ago

Hmm, what do you mean?


a month 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 month ago

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


a month ago

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


a month ago

!s


Status changed to Solved uxuz about 1 month ago