12

I generated an app using create-react-app and I want to use crypto-js I am getting the following error

crypto.sha256() is not a function

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
Ryan
  • 5,229
  • 1
  • 20
  • 31

1 Answers1

25

You gotta install crypto-js using

npm install crypto-js

In your js files, you have to import module you wanna use

import sha256 from 'crypto-js/sha256';

Now you can use those functions

sha256(nonce + message);

You have do define message and nonce before this

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
  • Thanks, can you please explain to me why doesn't `import * as crypto from crypto-js` work? – Ryan Dec 15 '17 at 15:17
  • @RyanDsouza, in-order to do `import *`, all modules has to be exported. That's not the case with `crypto-js` if you look at the code for `sha256.js`, https://github.com/brix/crypto-js/blob/develop/src/sha256.js. They're just defining a function . Check out my other answer about how export and import works in node https://stackoverflow.com/questions/21117160/what-is-export-default-in-javascript/43951115#43951115 – sudo bangbang Dec 16 '17 at 03:21