How to HMAC-S256 encrypt a string in Node JS

I am pretty new to Node.

I am trying to get the following PHP code

$signature = base64_encode(hash_hmac('sha256', $canonicalString, $secret, true));
$signature = rtrim($signature,"=");

to work in node . Currently I have the following code implementation in node.

  return crypto.createHmac('sha256', key)
    .update(message)
    .digest('base64');

Still the service not getting authenticated.

Any help is much appreciated. Been stuck at the same line of code for more than a day.

Thank you