Using process.env variables in Eleventy
24 Jun 2019
In Eleventy (the static site builder used to create this site), you can access Node environment variables (process.env.MY_VARIABLE
) in your templates. Neat! But how?
# How to set up the variables for use in templates
The Eleventy docs provide a code snippet to use:
module.exports = {
myVariable: process.env.MY_VARIABLE
};
One thing you might overlook is the filename - this doesn't go in your .eleventy.js
file, but in the special _data/
folder at a Javascript file named of your choosing. I chose to call mine _data/processEnv.js
.
# How to access your variable in a template
Once you have an object exported from a Javascript file in _data
, that object is available in your templates as filename.variablename
. So in my Liquid (markdown) templates, I write: {{ processEnv.myVariable }}
to display the value stored at process.env.MY_VARIABLE
.
# See it in action
I'm using this technique in the source code of another post on this site, so check it out! You should be able to poke around the full source of my project to see how it's configured. The variable I'm using there is called
{{ processEnv.values.PROJECT_DOMAIN }}
.