Skip to navigation Skip to main content
11ty Logo Sustainability Fundraising
Eleventy
Eleventy Documentation
Stable
2.0.1
Canary
3.0.0-alpha.17
Toggle Menu
Eleventy 1.93s
Astro 22.90s

Environment Variables

Contents

You can set and use your own environment variables in your projects. They will be available in your code via Node.js’ process.env property.

These are typically used for setting your deployment context and private API keys. This is also the approach used to enable DEBUG mode.

INFO:
Note that Eleventy exposes environment variables only to JavaScript files that are evaluated during the build time. This includes the config file and all JavaScript files required from there, JavaScript data files, etc. Environment variables are not available in the templates. You need to expose them yourself. For example, using a Global Data file.

Setting your own

Via .env file

For private keys and other sensitive information, you’ll want to create a .env file and use the dotenv package to setup those values.

WARNING:
Make sure you add .env to your .gitignore file. Do not commit your .env file to your repository!!

Via the command line

macOS or Linux (et al)

MY_ENVIRONMENT=production npx @11ty/eleventy

Windows cmd.exe

set MY_ENVIRONMENT=production & npx @11ty/eleventy

Windows Powershell (default in VS Code)

$env:MY_ENVIRONMENT="production"; npx @11ty/eleventy

Cross Platform npm scripts

Use the cross-env package to compatibly set your environment variables cross-platform.

npm install cross-env
Filename package.json
{
"scripts": {
"build:prod": "cross-env MY_ENVIRONMENT=production npx @11ty/eleventy"
}
}

Use Case Ideas

Eleventy Supplied

Node.js exposes environment variables under process.env.

Eleventy also supplies its own Eleventy-specific environment variables, usually intended for more advanced use cases. You can use these in your configuration or in data files as needed.

  • process.env.ELEVENTY_ROOT Added in v1.0.0 the absolute path to the directory in which you’ve run the Eleventy command.
  • process.env.ELEVENTY_SOURCE Added in v1.0.0 is the method in which Eleventy has run, current either cli or script.
  • process.env.ELEVENTY_SERVERLESS Added in v1.0.0 is set to true (String) if Eleventy is running in serverless mode. If Eleventy is not running in serverless mode—due to Node forcing environment variables to be strings—this variable will not exist.
  • process.env.ELEVENTY_RUN_MODE Added in v2.0.0 is one of build, serve, or watch.
  • process.env.ELEVENTY_VERSION Pre-release only: v3.0.0-alpha.6 the current version of Eleventy (e.g. "3.0.0-alpha.5").

Disable Colors

Node.js supports a NODE_DISABLE_COLORS environment variable that will disable colorized text in the terminal output.

macOS Linux Windows Cross Platform
NODE_DISABLE_COLORS=1 npx @11ty/eleventy
$env:NODE_DISABLE_COLORS="1"; npx @11ty/eleventy

Or with the older cmd.exe:

set NODE_DISABLE_COLORS=1 & npx @11ty/eleventy
npx cross-env NODE_DISABLE_COLORS=1 npx @11ty/eleventy

Use the cross-env package to compatibly set your environment variables cross-platform.


Other pages in Using Data: