TypeError: crypto.createHmac is not a function

Viewed 1411

I have been trying to run integ tests and I am running into below error:

2022-03-26T18:51:12.446Z cypress:network:agent got family { family: 4, href: 'https://wepapi.com/api/session-status' }
  1) "before all" hook for "should login"
  0 passing (345ms)
  1 failing
  1) Login
       "before all" hook for "should login":
     TypeError: crypto.createHmac is not a function
Because this error occurred during a `before all` hook we are skipping all of the remaining tests.

We are using Crypto to generate Auth Lambda Version.

import Crypto from 'crypto';
import fs from 'fs';

export const getSha256FromFile = (filePath: string): string =>
Crypto
    .createHash('sha256')
    .update(fs.readFileSync(filePath))
    .digest('hex')

In auth Lambda File:

const codeSha256 = getSha256FromFile(AUTH_LAMBDA_CODE_FILE);

    this.version = authLambda.addVersion(
      `AuthLambdaVersion_${codeSha256}`,
      codeSha256

I am not even using crypto.createHmac function. Not sure what is going on.

Update

I just realized we are using AWS4 Sign functionality to sign requests. Kind of like this:

aws4.sign(requestOptions, {
  secretAccessKey: "<your-secret-access-key>",
  accessKeyId: "<your-access-key-id>",
  sessionToken: "<your-session-token>"
})

And Sign functionality uses crypto functions.which is giving this error. but I am still not sure how to fix it

1 Answers

The correct way to import the crypto module is the following:

import * as crypto from "crypto"

Or you could just import the functions

import { createHmac } from "crypto" 
Related