By jak6jak
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?
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!
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:
DATABASE_URL
${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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.