Runtime environment variable with railpack

Hi! I'm deploying an API made with Vite so I decided to use Railpack for this. I'd like some environment variables to be available at runtime, not only build time. Is that possible and if so, how?

Thanks!

Solved$10 Bounty

7 Replies

a year ago

Hey, (environment/service) variables defined on Railway are available during build and at runtime. You can define them through the service's Variables tab.

When defined, variables are made available to your application as environment variables in the following scenarios:

  • The build process for each service deployment.
  • The running service deployment.
  • The command invoked by railway run <COMMAND>
  • The local shell via railway shell

...

Documentation: https://docs.railway.com/reference/variables#how-it-works


process.env is an empty object. Do I need to manually forward env vars? Eg.

-pnpm api start
+FOO=$FOO BAR=$BAR pnpm api start

FYI I'm defining a start command in a railway.json file


florian-lefebvre

`process.env` is an empty object. Do I need to manually forward env vars? Eg. ```diff -pnpm api start +FOO=$FOO BAR=$BAR pnpm api start ``` FYI I'm defining a start command in a `railway.json` file

a year ago

I am not sure if Vite is at fault here, but have you tried using import.meta.env plus VITE_ prefix for your variables instead of process.env? Vite Env Documentation: https://vite.dev/guide/env-and-mode.


I'm not using import.meta.env on purpose for the API because it inlines variables in the build output, which is unsecure in a server side context. Locally I use process.env and dotenv successfully


I tried with nixpacks too but it doesn't seem to change anything


It turns out it was indeed caused by vite because it was turning process.env into

var define_process_env_default = {};

The solution was to force it to leave it as is:

  define: {
    'process.env': 'process.env',
  },

Thanks for the help!


florian-lefebvre

It turns out it was indeed caused by vite because it was turning process.env into ```ts var define_process_env_default = {}; ``` The solution was to force it to leave it as is: ```ts define: { 'process.env': 'process.env', }, ``` Thanks for the help!

a year ago

You are welcome!


Status changed to Open uxuz about 1 year ago


Status changed to Solved uxuz about 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...