Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import crypto-js in an angular 2 project (created with angular-cli)

I'm trying to import crypto-js in my angular2 project.

I followed several SO questions and also angular-cli guide, but at the end I still have the error Cannot find module 'crypto-js'

What I tried :

npm install crypto-js --save

and

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

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

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 the file src/system-config.ts

const map: any = {
    'crypto-js': 'vendor/crypto-js'
};

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

After using

import * as CryptoJS from 'crypto-js';

I still have my error. Did I miss something ?

Thanks

like image 779
Greg Avatar asked Jul 20 '16 11:07

Greg


People also ask

How do you use CryptoJS with angular 4?

Install using NPM and import below statement in you component file. npm install crypto-js import * as crypto from 'crypto-js'; now you can use crypto in your component file. Save this answer.

How do you use CryptoJS in angular 9?

Angular 9 code:import * as CryptoJS from 'crypto-js'; encryptionKey: any = 'secret key string'; let encryptedStr = CryptoJS. AES. encrypt('123456', this. encryptionKey.

How do you define CryptoJS?

CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.

How use crypto-JS in react JS?

To encrypt and decrypt data, simply use encrypt() and decrypt() function from an instance of crypto-js. var bytes = CryptoJS. AES. decrypt(ciphertext, 'my-secret-key@123');


3 Answers

Ok I got it. I just download the DefinitelyTyped file in typings/crypto-js/ and then I add the line /// <reference path="../../typings/crypto-js/crypto-js.d.ts" /> before importing CryptoJS.

like image 132
Greg Avatar answered Oct 15 '22 01:10

Greg


This may help you:


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

then:

import * as CryptoJS from 'crypto-js';

or

import CryptoJS = require('crypto-js');
like image 27
Weslley De Souza Avatar answered Oct 15 '22 02:10

Weslley De Souza


You can try following as a solution:

1. npm install --save @types/crypto-js


2. import { AES } from "crypto-js";


3. AES.encrypt('my message', 'secret key');
like image 44
zjx Avatar answered Oct 15 '22 02:10

zjx