Runtime environment variable with railpack

florian-lefebvre
HOBBY

4 months ago

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

4 months 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


florian-lefebvre
HOBBY

4 months ago

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.-pnpm api start +FOO=$FOO BAR=$BAR pnpm api startFYI I'm defining a start command in a railway.json file

4 months 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.


florian-lefebvre
HOBBY

4 months ago

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


florian-lefebvre
HOBBY

4 months ago

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


florian-lefebvre
HOBBY

4 months ago

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 intovar define_process_env_default = {};The solution was to force it to leave it as is:define: { 'process.env': 'process.env', },Thanks for the help!

4 months ago

You are welcome!


Status changed to Open uxuz 4 months ago


Status changed to Solved uxuz 4 months ago