Import crypto-js into an angular 2 project (created using angular-cli)

I am trying to import crypto-js into an angular2 project.

I have completed some SO questions as well as angular-cli guide , but in the end I still have an error. Cannot find the module 'crypto-js'

What I tried:

npm install crypto-js --save

and

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

then I changed the angular-cli-build.js file

 var Angular2App = require('angular-cli/lib/broccoli/angular2-app'); module.exports = function(defaults) { return new Angular2App(defaults, { vendorNpmFiles: [ 'systemjs/dist/system-polyfills.js', 'systemjs/dist/system.src.js', 'zone.js/dist/**/*.+(js|js.map)', 'es6-shim/es6-shim.js', 'reflect-metadata/**/*.+(ts|js|js.map)', 'rxjs/**/*.+(js|js.map)', '@angular/**/*.+(js|js.map)', 'crypto-js/**/*.+(js|js.map)' ] }); }; 

and src / system-config.ts file

 const map: any = { 'crypto-js': 'vendor/crypto-js' }; /** User packages configuration. */ const packages: any = { 'crypto-js': { format: 'cjs' } }; 

After use

import * as CryptoJS from 'crypto-js';

I still have my mistake. Did I miss something?

thanks

+5
source share
4 answers

OK I understood. I simply upload the DefinitelyTyped file to typings / crypto-js / and then add the line /// <reference path="../../typings/crypto-js/crypto-js.d.ts" /> before importing CryptoJS .

0
source

This may help you:
https://github.com/Uisli21/SecureAngularLogin


 $ npm install crypto-js $ npm install @types/crypto-js --save-dev 

then

 import * as CryptoJS from 'crypto-js'; 

or

 import CryptoJS = require('crypto-js'); 
+7
source

Angular-cli still has some problems for integrating third-party plugins. So be sure to add it to index.html . Add in this way

 <script src="vendor/crypto-js/crypto-js.js"></script> 

I think this will solve your problem :)

Update

 const map: any = { 'crypto-js': 'vendor/crypto-js' }; /** User packages configuration. */ const packages: any = { 'crypto-js': { format: 'cjs', defaultExtension: 'js', main: 'crypto-js.js' } }; 
0
source

As a solution, you can try:

 1. npm install --save @types/crypto-js 2. import { AES } from "crypto-js"; 3. AES.encrypt('my message', 'secret key'); 
0
source

All Articles