Jacob Edie
Jacob Edie

Reputation: 79

How to use Digitalocean's app platform component environmental variables?

I am using Digital Ocean's app platform to host a NodeJS app. I do not understand the documentation for using environmental variables within my NodeJS code. https://www.digitalocean.com/docs/app-platform/how-to/use-environment-variables/#define-build-time-environment-variables when I try to add the environmental variable like I think the documentation wants me to I get a syntax error: mongoose.connect(${_self.DATABASE_URL}, {useNewUrlParser: true}); What is the correct usage?

Upvotes: 5

Views: 1380

Answers (1)

Arnar Ingi Gunnarsson
Arnar Ingi Gunnarsson

Reputation: 76

Step 1:

In your App settings you should find the App-Level Environment Variables.

You could set a variable there like this:

Keys: APP_LEVEL_EXAMPLE_VAR 
Values: rafiki

Don´t forget to save.

Now you have an App-Level Environment Variable.

Step 2:

In your app´s project.yml file, you can set a variable that reads the App-Level variable like this:

environment: { 
  EXAMPLE_VAR: ${APP_LEVEL_EXAMPLE_VAR}
}

Step 3:

Now you can access the value of APP_LEVEL_EXAMPLE_VAR in your app like this:

const value = process.env.EXAMPLE_VAR

Upvotes: 6

Related Questions