Home > other >  Cannot destructure property 'Sha256' of 'sha256_js_1.default' as it is undefined
Cannot destructure property 'Sha256' of 'sha256_js_1.default' as it is undefined

Time:07-22

I am currently doing some work on a AWS-Lambda and as part of that project I am using "@aws-crypto/sha256-js" in my project. When I bring in Sha256 as per the Amplify documentation https://docs.amplify.aws/lib/graphqlapi/graphql-from-nodejs/q/platform/js/#iam-authorization and deploy the lambda everything works as expected. However, when running my tests with Jest I am receiving the following error:

    TypeError: Cannot destructure property 'Sha256' of 'sha256_js_1.default' as it is undefined.

      4 | import { default as fetch, Request } from "node-fetch";
      5 | import crypto from "@aws-crypto/sha256-js";
    > 6 | const { Sha256 } = crypto;  

I have read quite a bit of documentation and questions/answers on this and I am marking it up as this particular package having some kind of issue or Jest has some kind of issue with this package, but I could be wrong.

The deployed lambda works as expected and TypeScript is giving me auto-complete for when I bring in {Sha256} from crypto. Any help in getting rid of this annoying error would be greatly appreciated!

CodePudding user response:

This error usually comes from not having "esModuleInterop": true set in your tsconfig.json file (more information can be found at this link: https://www.typescriptlang.org/tsconfig#esModuleInterop)

You can either set "esModuleInterop": true, or you can use the import star syntax:

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