Strapi v4 lifecycle hooks not triggered

System Information
  • Strapi Version: 4.5.6
  • Operating System: macOS 12.5.1
  • Database: SQLite
  • Node Version: 16.18.0
  • NPM Version: 8.19.2
  • Yarn Version:

Hi everybody,

I am trying to write a lifecycle afterCreate hook on a custom Order content type, but it nevers seems to be triggered when a new record is created. Here is the hook I created in ./src/api/order/content-types/order/lifecycles.js:

module.exports = {
  async afterCreate(event) {
    console.log('Hi here I am')

This log never gets called and so my business logic fails. The doc clearly tells me to create my hook this way, so is there maybe something I missed ?
Thank you,

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.


For me the solution that worked was

// Strapi object is injected globally
module.exports = {
  async afterCreate(event) {
    console.log("[Lifecycle][AfterCrete]", event);

using Strapi “4.6.0”

I followed all of this, but the hooks still dont trigger. Any idea what could cause this issue?

1 Like

There’s a difference when you have created a custom API manually versus creates the API via STRAPI. My problem was that I installed a BK from other DB because the table has 500+ fields, so manually I had to duplicated and modify a content-type, but lifecycles doesn’t worked. So, I grab only 15 fields and I had to do all the process via STRAPI and when I check the STRAPI files was all the same as my manually configuration. I don’t know what’s the difference between the manually creation and the STRAPI Automation creation files, but after that I was able to send emails in the afterUpdate() method.

I have my lifecyles.js file into the src/api/myapi/content-types/myapi/lifecycles.js