Strapi v4 lifecycle hooks not triggered

Ok so after discussing with the Strapi team on Discord, there are several points to be aware of in the version 4:

  1. When in a Typescript project, make sure your lifecycles file is also in Typescript, else it won’t get compiled
  2. The file must be named exactly lifecyles.ts (or .js according to your project)
  3. The correct syntax for writing a hook in a typescript v4 project is the following:
export default {
  async afterCreate(event) {
    // whatever you need to do
  }
}

And this should make your lifecycles hooks work.

Note: the strapi object is injected globally, so you don’t have to explicitly declare it in your hook. This question arose because my linter was throwing me an error having the strapi object not declared, but to actually fix this you have to rebuild your project and the error should disappear.

4 Likes