Service Environment Variables Not Accessible in Runtime (Next.js, Nixpacks)
klproductions1
HOBBYOP

7 months ago

Hi,

I'm deploying a Next.js application on Railway and encountering a persistent issue with service environment variables that I'm hoping to get some help with.

The Problem:

  • I have defined several environment variables (NOTION_API_KEY, ELEVENLABS_API_KEY, NOTION_DATABASE_ID, and a test variable RAILWAY_TEST_VAR) in the Railway service's 'Variables' tab. I've double-checked that the names and values appear correct in the UI.

  • However, these variables are consistently undefined when accessed via process.env within my application's runtime (after next start). Railway's own provided variables (like RAILWAY_SERVICE_NAME) are accessible as expected.

  • The application appears to start successfully (shows ✓ Ready in ...ms in deploy logs), but any API requests that depend on these environment variables fail with a 500 Internal Server Error on the client-side.

  • Crucially, none of the custom console.error logs I've added throughout the code (in API route handlers, initialization functions like getNotionClient, attempting to log Object.keys(process.env)) are showing up in the Railway deployment logs.

Troubleshooting Steps Taken:

  1. Variable Verification: Repeatedly verified variable names (case-sensitive) and values (ensuring no leading/trailing spaces or hidden characters, copying directly from sources like Notion). These keys work correctly in other environments (e.g., Vercel).

  2. Redeployments: Redeployed the service numerous times after verifying/re-entering variables.

  3. Diagnostic Logging: Added detailed console.error logs at various points to check variable accessibility, but these logs are not appearing in the deployment output. Logging Object.keys(process.env) confirmed the absence of user-defined variables.

  4. Dockerfile -> Railpack (Nixpacks) Migration: Switched the build method from Dockerfile to Railpack (Nixpacks) to rule out Dockerfile-specific issues.

  5. npm ci Error Resolution: Fixed package-lock.json synchronization issues that arose after switching to Railpack, ensuring dependencies install correctly.

  6. API Simplification: Temporarily removed all external dependencies (like Notion client calls) from a specific API route (/api/get-scripts) and made it return only static data. The 500 error persisted, and even the entry log for this simplified handler did not appear in the deployment logs.

  7. Build/Config Checks: Confirmed the project does not use Turborepo and has no relevant middleware configured.

It seems the core issue is that the environment variables defined in the Railway UI are not being injected into the service's runtime environment, even when using Nixpacks. Since the application starts but fails during request handling without any logs appearing, it's difficult to pinpoint the exact failure point.

Could anyone offer insights into why this might be happening? Are there specific Railway configurations (related to Nixpacks, runtime environments, variable scopes) that I might be overlooking?

Any advice or suggestions would be greatly appreciated.

Thanks

Solved

3 Replies

echohack
EMPLOYEE

7 months ago

Hiya,

First, please don't use AI to generate a report. It is insincere.

To address your issue, you will need to check your NextJS configuration to ensure that your console logs will emit in a production environment. By default NextJS configures production logs to not emit as a security feature. You may see your console logs in development but not in production because of this.

Your environment variables are certainly available in your application.


Status changed to Awaiting User Response Railway 8 months ago


Status changed to Solved echohack 8 months ago


echohack

Hiya,First, please don't use AI to generate a report. It is insincere.To address your issue, you will need to check your NextJS configuration to ensure that your console logs will emit in a production environment. By default NextJS configures production logs to not emit as a security feature. You may see your console logs in development but not in production because of this.Your environment variables are certainly available in your application.

klproductions1
HOBBYOP

7 months ago

Thanks a lot for your response. I really appreciate it.

And I'd like to apologize for using an AI to write the report. I believed I needed it since my English skills couldn't elaborate the situation I had enough.


Status changed to Awaiting Railway Response Railway 7 months ago


echohack
EMPLOYEE

7 months ago

Totally understand. Were you able to solve the issue?


Status changed to Awaiting User Response Railway 7 months ago


Status changed to Solved echohack 7 months ago


Loading...