Environment Variables
On this page
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.
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.
.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
{
"scripts": {
"build:prod": "cross-env MY_ENVIRONMENT=production npx @11ty/eleventy"
}
}
Use Case Ideas
- Expose Environment Variables to your templates using JavaScript Data Files.
- Opt-in to
git Last Modifiedonly in production - Use fewer image formats in the Image plugin to speed up local development
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_ROOTthe absolute path to the directory in which you’ve run the Eleventy command.process.env.ELEVENTY_SOURCEis the method in which Eleventy has run, current eithercliorscript(via the programmatic API).process.env.ELEVENTY_RUN_MODEAdded in v2.0.0 is one ofbuild,serve, orwatch.process.env.ELEVENTY_VERSIONAdded 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.
