How to use CryptoJS with Angular 2 and TypeScript in WebPack build environment?

17,498

Solution 1

typings install dt~crypto-js --global --save

Explanation:

Solution 2

Here is the simplest step-by-step install and using example (working in nativescript/typescript/angular project):

npm install crypto-js

then:

npm install --save @types/crypto-js

Import in some component:

import * as crypto from "crypto-js";

And use it:

crypto.DES.decrypt("Your secret", "YOUR_CRYPTO_KEY");

Solution 3

The crypto-js package in npm has no built-in types and no longer maintained.

You can try this, Witch is maintained by me, the same as crypto-js with TypeScript support and ES6 module: https://www.npmjs.com/package/crypto-es.

Share:
17,498
Ian Smith
Author by

Ian Smith

Updated on June 27, 2022

Comments

  • Ian Smith
    Ian Smith almost 2 years

    There are a whole bunch of questions around using the CryptoJS library with Angular 2 but most assume use of SystemJS and all seem out of date with the current version of TypeScript. Can anybody give clear, simple instructions on how to use the CryptoJS library with Angular 2 and TypeScript.

    I've installed CryptoJS using npm install crypto-js.

    I've tried the recommended typings install crypto.js --ambient --save but this simply gives a warning about --ambient being deprecated and to use --global instead.

    I've used --global instead but that then gives an error saying no typings were available!

    Any advice to someone new to WebPack, NodeJS AND TypeScript would be appreciated. I have the CryptoJS library installed under node_modules folder but any "sensible" attempts to "import" CryptoJS fail with "Module not found". enter image description here

  • Ian Smith
    Ian Smith over 7 years
    Thanks. It was the "dt~" I was missing. It makes sense once you realise that the "source" the message is warning about is added by prefixing with the ~ The complete answer as to how to use CryptoJS with Angular2 and TypeScript was hidden in this "import" statement I found elsewhere. Other answers implying import {CryptoJS} from 'crypto-js' wouid work just failed. import * as CryptoJS from 'crypto-js';
  • rzelek
    rzelek over 7 years
    You are totally right. I remember me having the same problems with dt~ prefixes. The import problems you are having are related to angular-cli, because they were using SystemJS prevously. The first way of importing was valid for SystemJS setups.
  • Torsten N.
    Torsten N. about 7 years
    This creates a folder for cryptoJS, but the index.js says that global exports are not possible unless at top level. I scoured the web for a solution/alternative to this problem, but I couldn't really find a conclusive answer. It's the same starting situation for me as for the OP: I have CryptoJS installed in node_modules, but can't import it. To add, I can't install as global module because: typings ERR! message Attempted to compile "crypto-js" as a global module, but it looks like an external modu le. You'll need to remove the global option to continue.
  • rzelek
    rzelek about 7 years
    Seems like crypto-js typings are not global anymore (I am not sure, haven't used typings in a while). Consider asking separate question. Also, if you run typescript >2.0, check out @types (stackoverflow.com/questions/39261204/typings-vs-types-npm-s‌​cope).
  • Scott R. Frost
    Scott R. Frost about 7 years
    @Arek-Żelechowski is correct, you should now use npm install --save @types/crypto-js
  • Jainam Jhaveri
    Jainam Jhaveri almost 7 years
    And how can we actually use it inside a component ?
  • Vlad
    Vlad over 5 years
    This way crashes for me in prod mode. I think there must be also included scripts
  • Joseph T F
    Joseph T F about 5 years
    I think you will end up with this issue github.com/angular/angular-cli/issues/…. Correct me if I am wrong?
  • Jiri Kralovec
    Jiri Kralovec about 4 years
    There is no need for using --save flag when installing types, why not install them only for development?
  • Sajib Acharya
    Sajib Acharya over 3 years
    While this is a good answer, please consider disclosing that you are the author of the library in your answer.
  • Entronad
    Entronad over 3 years
    Thanks for your advice! I shall disclose that.
  • Keith OYS
    Keith OYS almost 3 years
    This is working on Nativescript-vue as of today. Installed "crypto-js": "^3.3.0" - this version specifically because of this issue: github.com/brix/crypto-js/issues/256#issuecomment-585175130