[go: up one dir, main page]

Skip to navigation Skip to main content
Eleventy
Eleventy Documentation
Stable
3.0.0
Toggle Menu
Eleventy 1.93s
Gatsby 29.05s

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 environment variables are only available in JavaScript files in your project, evaluated at build time. This includes your config file, JavaScript data files, JavaScript templates, etc. To use environment variables in other template languages, you can use a Javascript 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 the absolute path to the directory in which you’ve run the Eleventy command.
  • process.env.ELEVENTY_SOURCE is the method in which Eleventy has run, current either cli or script.
  • process.env.ELEVENTY_RUN_MODE Added in v2.0.0 is one of build, serve, or watch.
  • process.env.ELEVENTY_VERSION Added in v3.0.0 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.

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: