简体   繁体   中英

How can I use Crypto built-in module from Node.JS in React Native Project?

I'm working on a React Native project and I need to implement authentication, with an encrypted password.

I want to use library Bcrypt but I have an issue. Bcrypt requires the Crypto module , which was a third-part library, but now it is a built-in module in Node.js .

When I try to use Bcrypt with const bcrypt = require('bcrypt') , it throws an error:

Unable to resolve module 'crypto' from 'node_modules/bcrypt/...'

I am using:

  • Node.js v13.0.1
  • NPM v6.12.0
  • react-native-cli v2.0.1
  • react-native v0.61.3
  • react v16.9.0

To manage this error, I went to Node.js documentation and tried the solution to know if the Crypto built-in module is available or not but it throws the same error.

Maybe I don't understand clearly what is a built-in module, is that a module which is included into the Node.js installer , so I don't have any NPM install crypto to do?

If it is the case, does anyone have an idea about why I can't use it in my project ?

I also tried the rn-nodeify module , with react-native-bcrypt and react-native-crypto , but in each case some packages weren't reached, like stream , VM , fs , etc.

Do you encountered the same problem and solved it?

Someone already answered that question here Bcrypt is not supported in Reactjs It's because bcrypt is written in C++. But now they have bcryptjs, the JS version of bcrypt. You can download it here https://www.npmjs.com/package/bcryptjs . Note that bcryptjs is slower than the C++ version - bcrypt, because "it is written in pure JavaScript and thus slower (about 30%), effectively reducing the number of iterations that can be processed in an equal time span."

I would request you to look into rn-nodeify library. It enables you to use core modules and npm modules in your react-native app.

Look into this answer rn-nodeify installation and start from Step 2 .

I hope i could help you.

https://github.com/margelo/react-native-quick-crypto I've used this lib the fix for endian.h is to rename it to <machine/endian.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM