New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I read from a .env file? #782
Comments
Take a look at https://www.npmjs.com/package/dotenv |
@michrome I'm using Sample env file: Data file: Links updated: 11/08/2021. |
Thank you @DirtyF and @chrisssycollins 🤝 For anyone else coming to this later, it's as simple as:
|
These example links no longer work. |
@davidrhoden There should be a few other references and examples using the dotenv library in this repo: Alternatively, I think you can check out an older branch to see those files now: |
@davidrhoden I've updated the links, thanks for flagging. |
Here's a little tutorial outlining what I did to access environment variables in First, add
Initialize in // Populates environment variables into process.env and makes it available in 11ty's global data.
require('dotenv').config();
eleventyConfig.addGlobalData('env', process.env); In your FOO=BAR
BAZ=QUX And in your templates, just access
Folks googling will still land on this page, so I figured I'd drop this here (at least for my future self). 😄 |
Fun fact, if you're using v1, you can use the const inspect = require("node:util").inspect;
require("dotenv").config();
module.exports = (eleventyConfig) => {
eleventyConfig.addFilter("inspect", (value) => inspect(value, {sorted: true}));
eleventyConfig.addGlobalData("env", process.env);
return {
dir: {
input: "src",
output: "www",
}
};
}; Then in your template it should still just be: <!-- src/index.njk -->
<p>FOO={{ env.FOO }}</p>
<p>BAZ={{ env.BAZ }}</p> OUTPUT<p>FOO=BAR</p>
<p>BAZ=QUX</p> Although this means ALL of your environment variables will end up in <pre data-debug>{{ env | keys | inspect | safe }}</pre> OUTPUT<pre data-debug>[
'APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL',
'BAZ',
...,
'npm_package_name',
'npm_package_version'
]</pre> If you just want the environment variables from the .env file and not the entire system/universe, you can use the eleventyConfig.addGlobalData("env", require("dotenv").config().parsed); OUTPUT<pre data-debug>[ 'BAZ', 'FOO' ]</pre> Here's my "final" .eleventy.js file: const inspect = require("node:util").inspect;
module.exports = (eleventyConfig) => {
eleventyConfig.addFilter("inspect", (value) => inspect(value, {sorted: true}));
eleventyConfig.addFilter("keys", obj => Object.keys(obj).sort());
eleventyConfig.addGlobalData("env", require("dotenv").config().parsed);
return {
dir: {
input: "src",
output: "www",
}
};
}; |
Oh, nice! That is more elegant. Even though I only just started, I was still on Not sure if it's best practice per se (since I don't think those I think I'll tweak my mini tutorial, too.... thanks! 🚀 |
Just some feedback that this was a helpful thread for my related but different use case. I assumed I could just require dotenv in my global JS data file, but the value outputted in the template was |
I know I can pass local environment variables to 11ty by running it with a syntax like
$ SECRET_TOKEN=xxxyyy npx @11ty/eleventy --serve
but what's the best way to pick this up from a local.env
file?Failing that, what's a good way to inject a secret value into
process.env
without committing the secret to source control?Thank you!
The text was updated successfully, but these errors were encountered: