How to use CryptoJS with Angular 2 and TypeScript in WebPack build environment?
Solution 1
typings install dt~crypto-js --global --save
Explanation:
-
--global
CryptoJS is global module -
dt~
The typings are inside DT repository: https://github.com/DefinitelyTyped/DefinitelyTyped -
--save
Because you want it to be stored insidetypings.json
, don't you?
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.
Ian Smith
Updated on June 27, 2022Comments
-
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 i
nstall 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".
-
Ian Smith over 7 yearsThanks. 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 over 7 yearsYou 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. about 7 yearsThis 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 about 7 yearsSeems 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-scope). -
Scott R. Frost about 7 years@Arek-Żelechowski is correct, you should now use
npm install --save @types/crypto-js
-
Jainam Jhaveri almost 7 yearsAnd how can we actually use it inside a component ?
-
Vlad over 5 yearsThis way crashes for me in prod mode. I think there must be also included scripts
-
Joseph T F about 5 yearsI think you will end up with this issue github.com/angular/angular-cli/issues/…. Correct me if I am wrong?
-
Jiri Kralovec about 4 yearsThere is no need for using --save flag when installing types, why not install them only for development?
-
Sajib Acharya over 3 yearsWhile this is a good answer, please consider disclosing that you are the author of the library in your answer.
-
Entronad over 3 yearsThanks for your advice! I shall disclose that.
-
Keith OYS almost 3 yearsThis 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