Unexpected error when implementing simple heartbeat

To make sure that I was not making an error, I created a new project and copied verbatim the code from the docs here: Heartbeats :: Internet Computer

That is, I have a single canister with code:

import Debug "mo:base/Debug";

actor Alarm {

  let n = 5;
  var count = 0;

  public shared func ring() : async () {
    Debug.print("Ring!");
  };

  system func heartbeat() : async () {
    if (count % n == 0) {
      await ring();
    };
    count += 1;
  }
}

And I get the following error:

type error [M0129], unexpected system method named heartbeat, expected preupgrade or postupgrade. 

Why am I getting this unexpected error?

Your dfx version is probably out-of-date.

If it’s up-to-date, then you probably need to restart your local dfx replica. Run dfx stop and then dfx start --clean.

1 Like

Thanks! It was a dfx update issue.