In my Angular 2 application (SystemJS module manager, Typescript as a scripting language) I need to import the npm module for encryption processing (either Crypto-JS; Forge-JS, or any other serving purpose)
In the case of CryptoJS , after installing through npm install *, I tried to add:
<script src="node_modules/crypto-js/crypto-js.js"></script>
in index: html .
In my service ( app / services / my-service.service.ts ) I import it through
import {CryptoJS} from 'node_modules/crypto-js/crypto-js.js' // or /aes.js --> same issue
However, the import does not work correctly, for example,
console.log(CryptoJS);
outputs undefined .
I also tried adding the module path to
System.config({
and import it into my service
import {CryptoJS} from 'cryptoJs';
While I'm not sure that what I should add to SystemJS configuration , none of the solutions I tried worked.
EDIT I also tried ...
// import ... as to overcome no default export import * as CryptoJS from 'node_modules/crypto-js/crypto-js.js';
but then
console.log(CryptoJS.);
doesn't give any AES / any method (my editor usually advises which methods I can use with autocomplete)
Thanks to the contributions of Thierry and PierreDuc, EDIT 2 makes it clear that typing and importing modules are unrelated concepts.
However, none of them work. This is what I did:
I downloaded the CryptoJS typing file , put it in typings / cryptojs / cryptojs.d.ts
Then i added
before typings / main.d.ts
Then I added crypto to the SystemJS map configuration:
cryptojs: "node_modules/crypto-js/crypto-js.js"
Finally, I tried to import cryptojs into my service
import CryptoJS from 'cryptojs'
As far as I can see, there are 2 problems:
- Typics do not load, because there is no autocomplete when I try to import the module (I also tried to restart the Angular 2 application). Perhaps I did not understand how to import external typing?
- the module is still not loaded, I see that using console.log (cryptojs) (nothing is printed, even undefined; hardly my previous attempts)
EDIT 3
Finally, I got the import, thanks to the advice of Thierry and PierreDuc (not sure what went wrong in the first place). However, I still have type problems.
Despite the fact that I put
directly in my service when I write
import CryptoJS from 'cryptojs';
just below this line, I have no autocomplete, and when I start the Angular 2 application using npm start ; I get the following error and the application does not start
app/services/user.service.ts(6,22): error TS2307: Cannot find module 'cryptojs'.
NOTE If I add cryptojs to the SystemJS configuration (but not a) and then write (without import)
console.log(CryptoJS.AES.encrypt('my message', 'secret key123').toString());
it just works, but I would rather solve the problems with type + import.