Home

Ignore _site Build Directory in Eleventy

How to ignore the build output when adding it to gitignore causes problems.

The other day I ran into a scenario with Eleventy in which having a .gitignore file without ignoring the build output directory (usually _site) caused the build to fail.

The error looked like this for me:

Unhandled rejection in promise ([object Promise]): (more in DEBUG output)
> Unexpected token =

`SyntaxError` was thrown:
    /path/to/node_modules/@11ty/eleventy/test/stubs/classfields-data.11ty.js:2
      data = {
           ^

    SyntaxError: Unexpected token =
...

Build Failure Cause

The reason this occurs is likely because Eleventy is trying to process files in the node_modules directory:

Paths listed in your project's .gitignore file are automatically ignored ...

If you do not have a .gitignore file in your project, the node_modules directory will be ignored automatically. This makes new Eleventy projects a little easier and helps developers new to Eleventy get ramped up easier too.

There's a goofy exception in here — if you have a .gitignore file and it has contents, but those contents do not include node_modules, then Eleventy will try to process files in node_modules.

The reason I came across this is because I was working in a space in which the project was inside a larger repository, where the root-level .gitignore had included node_modules, while my Eleventy project did not.

Easy Solution

The easy solution is to add node_modules to your .gitignore file:

.gitignore

node_modules/
_site/

Another Way to Solve

Having node_modules in your .gitignore file is usually the way to go — it's a best practice. But, if there's a reason you don't want to include it, there's still a way to get Eleventy to work.

The other approach is to add node_modules/ to .eleventyignore, which will automatically get picked up and ignored by Eleventy, but will still track those node_modules files with git (again, which you almost never want to do).

.eleventyignore

node_modules/

Let's Connect

Keep Reading

Add a Static Directory to an Eleventy Project

Copy static files from a directory into the root of the build directory with Eleventy.

Aug 17, 2020

The Jamstack Journey: A Guide on Transforming an Idea into a Website

It takes a lot to bring an idea to life on the web, even for the simplest of sites. Follow this guide for a detailed look at moving from concept to a website deployed to your domain.

Sep 08, 2021

2 Methods for Binding JavaScript Events with 11ty

When using raw JavaScript with 11ty, there are multiple approaches you can take for binding events, all without complicating the JavaScript you’re using.

Sep 29, 2022