a month ago
Pinned Solution
a month ago
turbo uses strict mode by default which filters out all env variables unless you explicitly list them. railway injects the variables fine but turbo blocks them
add this to your ROOT turbo.json (not in the apps folder, the main one at repo root):
{
"envMode": "loose"
}that's the quick fix. if you wanna do it properly instead of loose mode, you can list all your variables in turbo.json like:
{
"globalEnv": ["DATABASE_URL", "BETTER_AUTH_URL", "BETTER_AUTH_SECRET", "NODE_ENV", "PORT", etc...]
}loose mode is easier tho
10 Replies
a month ago
https://docs.railway.com/guides/variables#service-variables
They're available during the build process. Are you using Docker?
a month ago
I'm not using docker, I also noticed that railway provided variables are also missing there
console.log("ENVs:", process.env);
jigz
I'm not using docker, I also noticed that railway provided variables are also missing thereconsole.log("ENVs:", process.env);
a month ago
where exactly are you calling that console.log? is it in your package.json build script, in a config file that runs during build, or in your main app file? also are you using dotenv anywhere in your code (like require('dotenv').config() or import 'dotenv/config')?
a month ago
I'm using turborepo btw, using zod to validate variables
apps/this-app/src/env.ts
```
import path from "node:path"
import { config } from "dotenv"
import { expand } from "dotenv-expand"
import { z } from "zod"
expand(
config({
path: path.resolve(
process.cwd(),
process.env.NODE_ENV === "test" ? ".env.test" : ".env"
),
})
);
console.log("ENVs:", process.env);
```
a month ago
that's your issue, you're calling config() from dotenv which is overriding railway's injected variables. railway already injects all your variables directly into process.env, so when you call dotenv.config() it tries to load from a .env file and wipes out what railway gave you.
remove or wrap that dotenv stuff so it doesn't run on railway:
import path from "node:path"
import { config } from "dotenv"
import { expand } from "dotenv-expand"
import { z } from "zod"
if (process.env.NODE_ENV !== "production" && !process.env.RAILWAY_ENVIRONMENT) {
expand(
config({
path: path.resolve(
process.cwd(),
process.env.NODE_ENV === "test" ? ".env.test" : ".env"
),
})
);
}
console.log("ENVs:", process.env);railway provides a RAILWAY_ENVIRONMENT variable you can check for, or just skip dotenv in production
a month ago
I'm afraid that's not it, even NODE_ENV is missing, I'm looking through turborepo issues maybe it has it's own way of handling env
import path from "node:path";
import { config } from "dotenv";
import { expand } from "dotenv-expand";
import { z } from "zod";
console.log("ENVs:", process.env);
if (process.env.NODE_ENV !== "production" && !process.env.RAILWAY_ENVIRONMENT) {
expand(
config({
path: path.resolve(
process.cwd(),
process.env.NODE_ENV === "test" ? ".env.test" : ".env"
),
})
);
}
a month ago
Please see turbo's documentation.
https://turborepo.com/docs/crafting-your-repository/using-environment-variables#environment-modes
a month ago
turbo uses strict mode by default which filters out all env variables unless you explicitly list them. railway injects the variables fine but turbo blocks them
add this to your ROOT turbo.json (not in the apps folder, the main one at repo root):
{
"envMode": "loose"
}that's the quick fix. if you wanna do it properly instead of loose mode, you can list all your variables in turbo.json like:
{
"globalEnv": ["DATABASE_URL", "BETTER_AUTH_URL", "BETTER_AUTH_SECRET", "NODE_ENV", "PORT", etc...]
}loose mode is easier tho
ilyassbreth
turbo uses strict mode by default which filters out all env variables unless you explicitly list them. railway injects the variables fine but turbo blocks themadd this to your ROOT turbo.json (not in the apps folder, the main one at repo root):{ "envMode": "loose" }that's the quick fix. if you wanna do it properly instead of loose mode, you can list all your variables in turbo.json like:{ "globalEnv": ["DATABASE_URL", "BETTER_AUTH_URL", "BETTER_AUTH_SECRET", "NODE_ENV", "PORT", etc...] }loose mode is easier tho
a month ago
This is exactly what I was hoping you'd add on to my message!
ilyassbreth
turbo uses strict mode by default which filters out all env variables unless you explicitly list them. railway injects the variables fine but turbo blocks themadd this to your ROOT turbo.json (not in the apps folder, the main one at repo root):{ "envMode": "loose" }that's the quick fix. if you wanna do it properly instead of loose mode, you can list all your variables in turbo.json like:{ "globalEnv": ["DATABASE_URL", "BETTER_AUTH_URL", "BETTER_AUTH_SECRET", "NODE_ENV", "PORT", etc...] }loose mode is easier tho
a month ago
Super thanks, odd that in my previous host env variables were available even without envMode
Status changed to Solved brody • about 1 month ago
