Use env variable in app

I’m running a Node.js app and I have setup my env variable like this:

convox env set dbHost=domain1.com

But in my app when I try to access the env variable like this, like I would normally do for an env variable:

{
  host: process.env.dbHost
}

the env variable does not exist.

I checked in my Convox Console in the Racks/Apps/{theApp}/Environment tab and I see the variable being there. I even set NODE_ENV=production, but when I look at process.env I see NODE_ENV set to staging which is the default for my app.

I ran also

convox releases info

And I see under Env my env variables

How do you access the env variable?

Ok, I figured it out. In order to be able to use the env variable, the variable has to be declared in the convox.yml

services:
  web:
    build: .
    port: 3000
    environment:
      - NODE_ENV
      - dbHost
      - dbName
      - dbPass
      - dbUser
    health:
      grace: 5
      interval: 5
      path: /status
      timeout: 3
    domain:
      - "*.somedomain.com"

Yep, that’s right, and specified in the docs.
You can also use a wildcard if you want to bring in all the env vars if you wish: https://docs.convox.com/application/environment#wildcard

Hope that helps!
Ed

1 Like