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. 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:

  • 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

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel