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:
      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:



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).



Let's Connect

Keep Reading

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

Ignore Files in 11ty for Faster Development Builds

A super quick and simple way to speed up the development experience on 11ty sites as they grow in size and complexity.

May 05, 2022

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