Home
Support Forums

Eleventy events beforeBuild and afterBuild are not run on Netlify build

I use the Eleventy build events beforeBuild and afterBuild (https://www.11ty.dev/docs/events/) in my pipeline to process my assets, and they are working great when I run a local build, but they are not getting fired when the command is run by Netlify.

I inserted console logs at several steps to keep track of the build process. I can see the messages printed on my local console but can’t see any in the Netlify deploy logs. Moreover, my assets are dependant on the code that runs in those events to be inserted in the final build folder, and they are not in said folder once Netlify is done.

Site: https://hardcore-bartik-f05f55.netlify.app/

Deploy log:

7:08:40 PM: Build ready to start
7:08:42 PM: build-image version: b0258b965567defc4a2d7e2f2dec2e00c8f73ad6
7:08:42 PM: build-image tag: v3.4.1
7:08:42 PM: buildbot version: 0a44c930054be748eff0c581d346282fece89cc0
7:08:42 PM: Fetching cached dependencies
7:08:42 PM: Starting to download cache of 100.1MB
7:08:44 PM: Finished downloading cache in 2.06569409s
7:08:44 PM: Starting to extract cache
7:08:48 PM: Finished extracting cache in 3.891776889s
7:08:48 PM: Finished fetching cache in 6.03223225s
7:08:48 PM: Starting to prepare the repo for build
7:08:49 PM: Preparing Git Reference refs/heads/main
7:08:51 PM: Starting build script
7:08:51 PM: Installing dependencies
7:08:51 PM: Python version set to 2.7
7:08:51 PM: Started restoring cached node version
7:08:54 PM: Finished restoring cached node version
7:08:55 PM: v12.18.0 is already installed.
7:08:56 PM: Now using node v12.18.0 (npm v6.14.4)
7:08:56 PM: Started restoring cached build plugins
7:08:56 PM: Finished restoring cached build plugins
7:08:56 PM: Attempting ruby version 2.7.1, read from environment
7:08:57 PM: Using ruby version 2.7.1
7:08:58 PM: Using PHP version 5.6
7:08:58 PM: 5.2 is already installed.
7:08:58 PM: Using Swift version 5.2
7:08:58 PM: Started restoring cached node modules
7:08:58 PM: Finished restoring cached node modules
7:08:58 PM: Installing NPM modules using NPM version 6.14.4
7:09:03 PM: audited 686 packages in 4.04s
7:09:03 PM: 34 packages are looking for funding
7:09:03 PM: run npm fund for details
7:09:03 PM: found 0 vulnerabilities
7:09:03 PM: NPM modules installed
7:09:04 PM: Started restoring cached go cache
7:09:04 PM: Finished restoring cached go cache
7:09:04 PM: go version go1.14.4 linux/amd64
7:09:04 PM: go version go1.14.4 linux/amd64
7:09:04 PM: Installing missing commands
7:09:04 PM: Verify run directory
7:09:05 PM: ​
7:09:05 PM: ┌─────────────────────────────┐
7:09:05 PM: │ Netlify Build │
7:09:05 PM: └─────────────────────────────┘
7:09:05 PM: ​
7:09:05 PM: ❯ Version
7:09:05 PM: @netlify/build 5.0.2
7:09:05 PM: ​
7:09:05 PM: ❯ Flags
7:09:05 PM: deployId: 5f960578559b920007fcc51d
7:09:05 PM: mode: buildbot
7:09:05 PM: ​
7:09:05 PM: ❯ Current directory
7:09:05 PM: /opt/build/repo
7:09:05 PM: ​
7:09:05 PM: ❯ Config file
7:09:05 PM: /opt/build/repo/netlify.toml
7:09:05 PM: ​
7:09:05 PM: ❯ Context
7:09:05 PM: production
7:09:05 PM: ​
7:09:05 PM: ┌───────────────────────────────────┐
7:09:05 PM: │ 1. Build command from Netlify app │
7:09:05 PM: └───────────────────────────────────┘
7:09:05 PM: ​
7:09:05 PM: $ npm run build
7:09:05 PM: > eleventy-starter@1.0.0 build /opt/build/repo
7:09:05 PM: > set NODE_ENV=production&& rimraf _site && npx @11ty/eleventy
7:09:08 PM: Writing _site/fr/blogue/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/fr/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/fr/a-propos/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/fr/contact/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/fr/404/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/blog/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/about/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/contact/index.html from ./src/site/index.njk.
7:09:08 PM: Writing _site/404/index.html from ./src/site/index.njk.
7:09:08 PM: Benchmark (Data): ./src/site/_data/languages.js took 262ms (10.2%)
7:09:08 PM: Benchmark (Data): ./src/site/_data/pages.js took 824ms (31.9%)
7:09:08 PM: Benchmark (Data): ./src/site/_data/ui.js took 311ms (12.0%)
7:09:08 PM: Copied 6 files / Wrote 10 files in 1.65 seconds (165.0ms each, v0.11.0)
7:09:09 PM: ​
7:09:09 PM: (build.command completed in 3.8s)
7:09:09 PM: ​
7:09:09 PM: ┌─────────────────────────────┐
7:09:09 PM: │ Netlify Build Complete │
7:09:09 PM: └─────────────────────────────┘
7:09:09 PM: ​
7:09:09 PM: (Netlify Build completed in 3.8s)
7:09:09 PM: Caching artifacts
7:09:09 PM: Started saving node modules
7:09:09 PM: Finished saving node modules
7:09:09 PM: Started saving build plugins
7:09:09 PM: Finished saving build plugins
7:09:09 PM: Started saving pip cache
7:09:09 PM: Finished saving pip cache
7:09:09 PM: Started saving emacs cask dependencies
7:09:09 PM: Finished saving emacs cask dependencies
7:09:09 PM: Started saving maven dependencies
7:09:09 PM: Finished saving maven dependencies
7:09:09 PM: Started saving boot dependencies
7:09:09 PM: Finished saving boot dependencies
7:09:09 PM: Started saving go dependencies
7:09:09 PM: Finished saving go dependencies
7:09:09 PM: Build script success
7:09:09 PM: Starting to deploy site from ‘_site’
7:09:09 PM: Creating deploy tree
7:09:09 PM: Creating deploy upload records
7:09:09 PM: 10 new files to upload
7:09:09 PM: 0 new functions to upload
7:09:10 PM: Starting post processing
7:09:11 PM: Post processing - HTML
7:09:11 PM: Post processing - header rules
7:09:11 PM: Post processing - redirect rules
7:09:11 PM: Post processing done
7:09:12 PM: Site is live :sparkles:
7:09:26 PM: Finished processing build request in 44.207559966s

Here is the log I get when I run a build locally:

npm run build

eleventy-starter@1.0.0 build B:\web\Projects\eleventy-starter
set NODE_ENV=production&& rimraf _site && npx @11ty/eleventy

BEFORE BUILD
Writing _site/fr/blogue/index.html from ./src/site/index.njk.
Writing _site/fr/index.html from ./src/site/index.njk.
Writing _site/fr/a-propos/index.html from ./src/site/index.njk.
Writing _site/fr/contact/index.html from ./src/site/index.njk.
Writing _site/fr/404/index.html from ./src/site/index.njk.
Writing _site/blog/index.html from ./src/site/index.njk.
Writing _site/index.html from ./src/site/index.njk.
Writing _site/about/index.html from ./src/site/index.njk.
Writing _site/contact/index.html from ./src/site/index.njk.
Writing _site/404/index.html from ./src/site/index.njk.
AFTER BUILD
RUNNING POSTCSS production
POSTCSS MINIFY
Benchmark (Data): ./src/site/_data/languages.js took 1622ms (39.4%)
Benchmark (Data): ./src/site/_data/pages.js took 624ms (15.2%)
Benchmark (Data): ./src/site/_data/ui.js took 499ms (12.1%)
Copied 6 files / Wrote 10 files in 3.09 seconds (309.0ms each, v0.11.1)
POSTCSS DONE

As you can see, I get BEFORE BUILD and AFTER BUILD logs that correspond to the build events. These do not appear in the Netlify deploy log and the assets therefore do not get generated.

Could you tell me more about this command?

7:09:05 PM: > set NODE_ENV=production&& rimraf _site && npx @11ty/eleventy

I would use export NODE_ENV=production && ... since I don’t think that set makes a real environment variable.

I don’t know that is the whole problem, but may be related.

Could you also share your netlify.toml and other details, around the before and after build configuration settings? How are you trying to achieve that at Netlify, in more detail?

Hi!

I finally discovered what I was doing wrong. I manually set the NODE_ENV variable to production in my deploy settings, which is what my build process needs. However, for a reason that I can’t understand at all, Netlify does not install devDependencies when NODE_ENV is set to production. Since all the dependencies required for my build (including Eleventy) were listed in the devDependencies, nothing worked. I refactored my code a bit and removed the NODE_ENV variable and everything now builds properly.

However, for a reason that I can’t understand at all, Netlify does not install devDependencies when NODE_ENV is set to production.

This isn’t Netlify. This is how npm works :slight_smile: We just run it for you, don’t make decisions about what it does.

Oh really? Sorry I didn’t know that.

It would be nice to be able to override this somehow, but for now I was able to modify my code to take this into account.

Thanks for your support!

I guess I am not understanding why you are using NODE_ENV of production to install your devDependencies, but regardless, if you wanted that behavior to change, I think you’ll want to file a feature request on npm rather than Netlify (our service will continue to run npm as it is intended/provided from the creators). That team might even have heard something like this before, and have suggestions to work around it that are relevant to your use case :slight_smile:

I’d start with a feature request from here, were I wanting that change: https://github.com/npm/cli/issues/new/choose