Web3.js: Unable to resolve module `crypto` from bignumber.js

Created on 8 Mar 2017  路  27Comments  路  Source: ChainSafe/web3.js

web3 import causes unable to resolve module `crypto` from /node_modules/bignumber.js/bignumber.js error.

Import code:

import { default as Web3 } from 'web3';

web3 install command:

$ npm install web3 --save

Most helpful comment

So the short answer about React-Native is that it's not possible to include built-in Node packages in the app.

Here's a Stack Overflow question with an answer and a link to a React-Native issue.

My questions to you web3.js guys including @peculiarity @debris are:

  • Can we add an option to make web3.js work without relying on the built-in crypto Node package?

Looking at the web3.js code it appears it doesn't depend on crypto, but depends on debris/bignumber.js, which in turn depends on crypto.

debris/bignumber.js is a fork of MikeMcl/bignumber.js and it seems that MikeMcl/bignumber.js doesn't depend on crypto.

  • What are the reasons for web3.js using the debris fork of bignumber.js?

All 27 comments

I've resolved this by editing line 2677 of the file /node_modules/bignumber.js/bignumber.js as follows:

-if ( !crypto ) try { crypto = require('crypto'); } catch (e) {}
+if ( !crypto ) try { crypto = require('crypto-js'); } catch (e) {}

So it would appear this appear this could be an issue with the bignumber.js dependency, or the way it's being imported. I'm unable to open issues on their GitHub repo so posting here.

Well crypto and crypto-js are different things. crypto is module which is part of Node and crypto-js is separate library. If you check node's crypto documentation, you will find out that node might be configured in a way not to include crypto. Check how you built node.js or where you got it from.

Thanks @peculiarity

I've read the documentation link you've sent and believe I understand the situation now. However I still have the problem of not being able to find a node distribution that includes the crypto module.

I've tried installing node using all the following methods and I'm still seeing the error, after removing other versions, rm -rf node_modules, watchman watch-del-all, rm -rf $TMPDIR/react-native-packager-*, resetting Simulator, restarting etc, so I assume none of these builds include the crypto module. How then should I build or find a build of node that includes the crypto module?

sudo port install nodejs6

brew install node

n stable

https://nodejs.org/dist/v6.10.1/node-v6.10.1.pkg

Installing the JavaScript implementation of the crypto module with npm install --save crypto doesn't remove the error either.

Ok I have Windows 7 with node v6.2.2. Tried the example from the documentation in a local file and it finds the module. Can you try to create a separate simple file where you are invoking only the crypto module. See if it's found then it should be there and the problem might be indeed within web3.js impl.

Sample code from the doc.

```javascript
let crypto;
try {
crypto = require('crypto');
} catch (err) {
console.log('crypto support is disabled!');
}

Thanks @peculiarity

My current node installation is version 7.6.0 installed using n stable, which downloaded the distribution directly from the node website.

I've created a file index.js with the following content:

let crypto;
try {
  crypto = require('crypto');
} catch (err) {
  console.log('crypto support is disabled!');
}
const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
                   .update('I love cupcakes')
                   .digest('hex');
console.log(hash);

Running $ node index.js outputs:

c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e

So it looks like crypto is built-in to my installation of node..

I believe the problem is because I'm using Ractive-Native, which does not copy built-in node packages over to the app.

I've found a related question on Stack Overflow. It's 7 months old but unfortunately with no answer :(

@peculiarity would you have any idea what is the simplest way to include a node package that requires crypto module in a React Native app?

Sorry mate , can't help you with this one. Never used React nor React Native

So the short answer about React-Native is that it's not possible to include built-in Node packages in the app.

Here's a Stack Overflow question with an answer and a link to a React-Native issue.

My questions to you web3.js guys including @peculiarity @debris are:

  • Can we add an option to make web3.js work without relying on the built-in crypto Node package?

Looking at the web3.js code it appears it doesn't depend on crypto, but depends on debris/bignumber.js, which in turn depends on crypto.

debris/bignumber.js is a fork of MikeMcl/bignumber.js and it seems that MikeMcl/bignumber.js doesn't depend on crypto.

  • What are the reasons for web3.js using the debris fork of bignumber.js?

@mattvick If you find a way to use ethereum on iOS and Android give me feedback. I am currently running web3j and using it as a NativeModule from Java in my application but can't really find something to make it work with iOS.

Thanks and keep this up to date ;)

@acroix you can use rn-nodeify or ReactNativify to shim Node.js built in modules like Crypto. Then web3.js or other similar JavaScript libraries, that depend on Crypto etc, will work.

@mattvick did you succeed in making web3.js run on react native that way?
did you use the example transformer in ReactNativify?

@shayzluf I'm not currently using web3.js but I am using the example transformer in ReactNativify to shim crypto and a couple of other built-ins

As the resolutions field in package.json didn't seem to be working with my version of yarn, I replaced the following in yarn.lock:

"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
  version "2.0.7"
  resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"

With this:

[email protected], "bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
  version "4.0.2"
  resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.0.2.tgz#2d1dc37ee5968867ecea90b6da4d16e68608d21d"

I was able to get this working using rn-nodeify, react-native-crypto and react-native-randombytes :)

@corymsmith can I ping you privately for more details?

@shkfnly You bet! I just sent you a connection request on LinkedIn

@shkfnly Are you still looking for more details?

I just pushed a working example app to: https://github.com/bullish-ventures/react-native-web3-example

Actually, I just tried using Web3 without doing any of the above and it now seems to work since the bignumber dependency was fixed.

@corymsmith Thank you for the update

@corymsmith What version are you using that has the bignumber dependency fixed? I'm trying to build a react-native app as well and running into the same issue with web3 1.0.0-beta2.

@valashnez 0.19.0 but you may be able to use 0.20.0 as well

@mattvick I've got the exactly same issue with you but still cannot figure it out.
You are right, crypto is a built-in module in Node.js, which can't be import to React Native Apps.

Here is a workaround to force version 4.0.2 of the official bignumber.js package using Yarn which seems to work fine:
https://github.com/ethereum/web3.js/issues/576#issuecomment-310031753

Why is the bignumber.js fork needed?

If you're running into this issue while following the instructions from ReactNativify to inject crypto into the global scope, here's a quick fix.

Basically, the problem is that bignumber.js used to require() the crypto package, but the version used by web3 does not. So it expects it in the global scope. All you have to do is go to globals.js (or wherever your global.x definitions are) and change the definition for global.crypto to:

global.crypto = require('crypto');

@davidperrenoud @flotwig I try both your methods but no work. I use @mattvick 's way is ok.

-if ( !crypto ) try { crypto = require('crypto'); } catch (e) {}
+if ( !crypto ) try { crypto = require('crypto-js'); } catch (e) {}

It looks like this issue is not directly related to web3.js. If it is please write a comment here and I will reopen it.

Was this page helpful?
0 / 5 - 0 ratings