Environment variables not injected into Node.js process - only NODE_ENV works
filamirto
FREEOP

2 months ago

I have a Railway service running Node.js with the following start command:

cd backend && node node_modules/ts-node/dist/bin.js --transpile-only src/main.ts

Problem: None of the environment variables configured in the Variables tab are accessible via process.env in the running Node.js process. The only exception is NODE_ENV=production which Railway seems to inject automatically.

Evidence: I created a diagnostic endpoint /debug-env that returns the values of all variables. Result:

json

{
  "JWT_SECRET": "NO DEFINIDA",
  "GOOGLE_CLIENT_ID": "NO DEFINIDA", 
  "SMTP_USER": "NO DEFINIDA",
  "R2_BUCKET_NAME": "NO DEFINIDA",
  "RESEND_API_KEY": "NO DEFINIDA",
  "FRONTEND_URL": "NO DEFINIDA",
  "NODE_ENV": "production"
}

All variables are correctly configured in the Variables tab (verified by clicking the eye icon). I have tried:

  • Adding variables via Raw Editor (env format, no quotes)
  • Adding variables one by one via New Variable button
  • Clicking "Update variables" and redeploying manually
  • Changing Root Directory from "backend" to empty

Root directory: Empty (previously was "backend") Runtime: Node.js 22 Stack: Node.js + Express + TypeScript

Is there a known issue with variables not being passed when using cd subdirectory && in the start command?

$10 Bounty

3 Replies

Status changed to Awaiting Railway Response Railway 2 months ago


Status changed to Open chandrika 2 months ago


ilyass012
FREE

2 months ago

hey, add console.log(process.env) at the very top of your main.ts, before any imports or any other code runs, then check the railway deployment logs. that will tell you exactly whether railway is injecting the vars or not, and from there the actual cause becomes obvious


ilyass012
FREE

2 months ago

and can you share your main.ts file so i can see exactly what's happening at the top of your code before anything runs?


You need to use dotenv package to load your env variables when using node, because node doesn't load them by default.

1. Install the package: npm i dotenv

2. Add this line at the top of your main.ts: import 'dotenv/config'


Welcome!

Sign in to your Railway account to join the conversation.

Loading...