Eleventy: How do I read from a .env file?

Created on 20 Nov 2019  路  3Comments  路  Source: 11ty/eleventy

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!

education

Most helpful comment

Thank you @DirtyF and @chrisssycollins 馃 dotenv worked perfectly.

For anyone else coming to this later, it's as simple as:

  1. Install dotenv with $ npm install --save-dev dotenv.
  2. Add require('dotenv').config(); to your .eleventy.js file.

All 3 comments

@michrome I'm using dotenv in my project as @DirtyF suggested. You can see how it's used here:

Sample env file:
.env.sample

Data file:
_data/webmentions.js

Thank you @DirtyF and @chrisssycollins 馃 dotenv worked perfectly.

For anyone else coming to this later, it's as simple as:

  1. Install dotenv with $ npm install --save-dev dotenv.
  2. Add require('dotenv').config(); to your .eleventy.js file.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

aaronstezycki picture aaronstezycki  路  3Comments

nebrelbug picture nebrelbug  路  3Comments

matt-auckland picture matt-auckland  路  3Comments

DirtyF picture DirtyF  路  3Comments

halas picture halas  路  4Comments