Search code examples
typescriptencryptioncryptojslit-element

Non Angular : Uncaught (in promise) TypeError: Cannot read property 'CryptoJS' of undefined


I'm trying to use crypto-js (https://www.npmjs.com/package/crypto-js) for obvious reasons. Mine is Non-Angular, LitElement, Typescript, Web Component application. While running in local getting following error "Uncaught (in promise) TypeError: Cannot read property 'CryptoJS' of undefined".

Code for the same

import * as CryptoJS from 'crypto-js';
export class TestClass {
public encrypt() {
  console.log(CryptoJS.AES.encrypt('key', 'somevalue').toString());
 }
}

If i debug index.js file /nodemodules/crypto-js/index.js root is coming as undefined

; (function (root, factory, undef) {
// Some code
    // Global (browser)
root.CryptoJS = factory(root.CryptoJS); // getting root as undefined

Not sure, what whats the missing part. The same code works fine in Angular project.


Solution

  • Anyone struggling with importing crypto-js in typescript(as its in UMD format), can use crypto-es package. Its available in ES format.

    https://www.npmjs.com/package/crypto-es