Question

Can someone give example of using App platform's environmental variables in node.js

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?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
February 5, 2021
Accepted Answer

👋🏼 @jak6jak

The “bindable environment variables” are available for use in the app config. You’ll still need to add the actual environment variables to your app/component config, but you can use the templates as part of the values.

For example, if you have an app with a Node.js Service component and a database component, you can add an environment variable to the service component with:

  • Key: DATABASE_URL
  • Value: ${db.DATABASE_URL}

${db.DATABASE_URL} will be replaced by the database connection string before it is passed to the Node.js app. Then, in your code, you can use it just like a regular environment variable like so:

mongoose.connect(process.env.DATABASE_URL, {useNewUrlParser: true});

The db in ${db.DATABASE_URL} must match the database component’s name, so if it’s different, you’ll need to replace it. You can find the component’s name in the control panel under Apps -> Components -> Info.

If you haven’t connected a database to your app yet, see How to Manage Databases in App Platform.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.