"TypeError: hashConstructor is not a constructor", error when using "@aws-sdk/signature-v4" on AWS-Lambda

Viewed 99

I am developing a lambda in TypeScript that uses the "@aws-sdk/signature-v4" package and when I attempt to run the deployed lambda in the lambda console I receive the following error:

  "errorType": "TypeError",
  "errorMessage": "hashConstructor is not a constructor",
  "trace": [
    "TypeError: hashConstructor is not a constructor",
    "    at getPayloadHash (/var/task/node_modules/@aws-sdk/signature-v4/dist-cjs/getPayloadHash.js:17:26)",
    "    at SignatureV4.signRequest (/var/task/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:93:71)"
  ]
}

So far I have looked into the known issues for this package and I came across this https://github.com/aws/aws-sdk-js-v3/issues/3590 on Github. I am also using the "@aws-crypto/sha256-js" package which is supposed to offer a workaround, but so far doesn't seem to have had any effect on this error. "esModuleInterop": true is set in my tsconfig.json since "@aws-crypto/sha256-js" is a commonJS module. Any help would be appreciated!

How the packages are being imported:

import * as crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto;

UPDATE

I have logged out the following SignatureV4 object

const signer = new SignatureV4({
    credentials: defaultProvider(),
    region: AWS_REGION,
    service: "appsync",
    sha256: Sha256,
  });

and I am seeing that Sha256 is coming through as being undefined. I am trying to see why this is the case.

SOLVED

I had to change the way I was importing the packages, pretty dumb mistake and I hope by posting this others will avoid doing what I did.

The way that worked

import crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto;

The way that didn't work is at the beginning of this post.

1 Answers

Import the packages like this

import crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto;

And not like this

import * as crypto from "@aws-crypto/sha256-js";
const { Sha256 } = crypto; 
Related