web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

web3 import in react-native

Open flaviouk opened this issue 9 years ago • 41 comments

// Versions:
"web3": "1.0.0-beta.18"
"react-native": "0.46.4"
// Error:
Bundling `index.android.js`  0.0% (0/1110), failed.
Error: EISDIR: illegal operation on a directory, read
    at Object.fs.readSync (fs.js:681:18)
    at Object.readSync (ProjectName/node_modules/graceful-fs/polyfills.js:138:28)
    at tryReadSync (fs.js:542:20)
    at Object.fs.readFileSync (fs.js:585:19)
    at Module._readSourceCode (ProjectName/node_modules/metro-bundler/build/node-haste/Module.js:205:29)
    at Module._getCacheProps (ProjectName/node_modules/metro-bundler/build/node-haste/Module.js:395:29)
    at Module._readFromTransformCache (ProjectName/node_modules/metro-bundler/build/node-haste/Module.js:345:29)
    at Module.readCached (ProjectName/node_modules/metro-bundler/build/node-haste/Module.js:332:19)
    at ResolutionRequest.preprocessModule (ProjectName/node_modules/metro-bundler/build/node-haste/DependencyGraph/ResolutionRequest.js:380:27)
    at ResolutionRequest._preprocessPotentialDependencies (ProjectName/node_modules/metro-bundler/build/node-haste/DependencyGraph/ResolutionRequest.js:364:27)

Once I import web3 in my project I get this error, not sure how to fix it, ideas?

flaviouk avatar Sep 06 '17 15:09 flaviouk

I'm getting the same error even with version 0.20.2

"react": "16.0.0-alpha.12",
"react-native": "0.48.3",
"web3": "^0.20.2"

binhndicts avatar Sep 20 '17 03:09 binhndicts

@binhnd-socicom forgot I opened this, so the problem was that the react-native packager doesn't come with some node built in modules, you need to install node-libs-browser and then add to the root of your project:

// rn-cli.config.js
const extraNodeModules = require('node-libs-browser')

module.exports = {
  extraNodeModules
}

flaviouk avatar Sep 20 '17 11:09 flaviouk

@flaviocarvalho can you tell how to fix this. I tried this

//rn-cli.config.js
const extraNodeModules = require('node-libs-browser');
module.exports = {
  extraNodeModules
}
///App.js
....
require('./rn-cli.config');
....

but I am getting error require.resolve is not a function in require.resolve('/assert')

TonyFrancis avatar Oct 24 '17 05:10 TonyFrancis

@TonyFrancis You don't need to import the config on your app, it's a special filename that's gonna be used to configure your rn packager

flaviouk avatar Oct 24 '17 09:10 flaviouk

@flaviocarvalho I tried them I get error Requiring module "react-native-crypto/index.js" which threw an exception: TypeError: undefined is not an object(evaluating 'RNRandomBytes.seed') I am using create-react-native-app

TonyFrancis avatar Oct 24 '17 10:10 TonyFrancis

@TonyFrancis You might need to eject then, try with eject, and worst case scenario try react-native init

flaviouk avatar Oct 24 '17 10:10 flaviouk

@flaviocarvalho It didn't worked

TonyFrancis avatar Oct 24 '17 10:10 TonyFrancis

I created a step-by-step simple guide on how I set up web3.js with Create React Native App:

https://gist.github.com/dougbacelar/29e60920d8fa1982535247563eb63766

dougbacelar avatar Oct 24 '17 23:10 dougbacelar

@dougbacelar thanks it helped.

TonyFrancis avatar Oct 25 '17 04:10 TonyFrancis

@dougbacelar now when I try web3.eth.accounts.create I get error Unknown module crypto. CAn you help me ?

TonyFrancis avatar Oct 25 '17 08:10 TonyFrancis

@flaviocarvalho Thank you for your instructions. But I get the following error. Unable to resolve module crypto from path/to/module/node_modules/web3-eth-accounts/src/index.js: Modules does not exist in the module map.

As I understand the sub-module's mapping is not carried forward to dist folder.

Can you please help.

KSanthanam avatar Nov 06 '17 21:11 KSanthanam

Hi, Using [email protected] [email protected] I have this error:

Bundling `index.js`  [development, non-minified]  98.7% (1059/1066), failed.
error: bundling failed: Error: require() must have a single string literal argument: /Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/web3-eth-accounts/node_modules/eth-lib/lib/bytes.js:8
    at pushDependency (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/metro-bundler/src/JSTransformer/worker/extract-dependencies.js:44:13)
    at CallExpression (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/metro-bundler/src/JSTransformer/worker/extract-dependencies.js:59:9)
    at NodePath._call (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitSingle (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/babel-traverse/lib/context.js:108:19)
    at TraversalContext.visit (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/babel-traverse/lib/context.js:192:19)
    at Function.traverse.node (/Users/mokhtarbacha/Documents/workingMobileAppRNodify3/node_modules/babel-traverse/lib/index.js:114:17)

ahmb84 avatar Nov 13 '17 11:11 ahmb84

@TonyFrancis Whats the error you are getting? I will create a repository with both working together soon. Maybe that will help you

dougbacelar avatar Nov 13 '17 12:11 dougbacelar

@TonyFrancis I'm getting the require() must have a single string literal argument. It's related to dynamic import. https://github.com/facebook/metro-bundler/issues/65 https://github.com/facebook/react-native/issues/16216 And it's ok for create a repo

ahmb84 avatar Nov 13 '17 12:11 ahmb84

i have this issue too. i ended up patching modules myself and now it compiles. I'll test to see if anything was affected in web3

there are two files to patch

node_modules/eth-lib/lib/bytes.js and node_modules/web3-eth-accounts/node_modules/eth-lib/bytes.js

instead of

require("c" + "rypto")

i wrote:

require("crypto")

azat-co avatar Nov 20 '17 09:11 azat-co

My problem went away when I used web3@^0.19.1 and added "react-native" section to package.json. Hope it helps someone.

"react-native": { "zlib": "browserify-zlib", "console": "console-browserify", "constants": "constants-browserify", "crypto": "react-native-crypto", "dns": "dns.js", "net": "react-native-tcp", "domain": "domain-browser", "http": "react-native-http", "https": "https-browserify", "os": "react-native-os", "path": "path-browserify", "querystring": "querystring-es3", "fs": "react-native-level-fs", "_stream_transform": "readable-stream/transform", "_stream_readable": "readable-stream/readable", "_stream_writable": "readable-stream/writable", "_stream_duplex": "readable-stream/duplex", "_stream_passthrough": "readable-stream/passthrough", "dgram": "react-native-udp", "stream": "stream-browserify", "timers": "timers-browserify", "tty": "tty-browserify", "vm": "vm-browserify" },

KSanthanam avatar Nov 20 '17 10:11 KSanthanam

@KSanthanam yes, the old version doesn't have this issue. it's the new version 1.x which has the problem

azat-co avatar Nov 30 '17 17:11 azat-co

@azat-co Yes. So I was forced to go back to [email protected]. Sad :(

KSanthanam avatar Nov 30 '17 21:11 KSanthanam

@azat-co's patch above worked for me too and using [email protected]. I was able to compile and get latest block right after.

web3.eth.getBlock('latest').then(console.log)

5:55:05 PM: Finished building JavaScript bundle in 6010ms 5:55:07 PM: Running app on Android SDK built for x86 in development mode

5:55:08 PM: Object { 5:55:08 PM: "difficulty": "3570652219", 5:55:08 PM: "extraData": "0x4554482e45544846414e532e4f52472d3132383643423442", 5:55:08 PM: "gasLimit": 4712394, 5:55:08 PM: "gasUsed": 239606, 5:55:08 PM: "hash": "0xddf88973e56d96f3e97f7098e82f52d0a70c00a81afae253e805ae5004c04835", 5:55:08 PM: "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 5:55:08 PM: "miner": "0xbbF5029Fd710d227630c8b7d338051B8E76d50B3", 5:55:08 PM: "mixHash": "0xc35ec20af3df0b4e13703c74dac3339ba8846a6aafe50bc27c177acbf05d15cb", 5:55:08 PM: "nonce": "0x5ff4a142ea0daccc", 5:55:08 PM: "number": 2412642, 5:55:08 PM: "parentHash": "0xb5049d4c8056aff5fdd42f4541bfcbfbddde458b1de5080e0da31e7c604aaee6", 5:55:08 PM: "receiptsRoot": "0xd9bb38965f4d5b1a3b44c2888f5c949fd65f2689c048c8aa8218c91b2ea1c105", 5:55:08 PM: "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", 5:55:08 PM: "size": 1765, 5:55:08 PM: "stateRoot": "0x5d4f105fcf237e6b86210535754e4a26df0afe1b004db1216fa31a608c198f74", 5:55:08 PM: "timestamp": 1515279263, 5:55:08 PM: "totalDifficulty": "7355460463617464", 5:55:08 PM: "transactions": Array [ 5:55:08 PM: "0x0ca3964f34a954a225c8862cd9c6561bfe6c7b57ebedc7d9cde358f9a06c7ab9", 5:55:08 PM: "0xe09d137ff15b3f521d3755cbd83286cf58d1d11e8caf039f85d69fcd722cf008", 5:55:08 PM: "0x29faf2430cfe6eae86dde3aeff4ebd309c3753a122b314645c562e65feeda396", 5:55:08 PM: "0x79e228447d35df3887626778145509ccc05d50ea6fa99f284d11846827c51862", 5:55:08 PM: "0xc71b1b501036439f9ae1933d51984ad08a912d6fd29c477d715fe05f9d39ae1b", 5:55:08 PM: "0x26da32ca38333fc7473adf7cc1b2afef48a08d1f5df1b7a89ec1e32eb0dc7fca", 5:55:08 PM: "0x181a3975912160cb7a1d9093661cadc2c6c31ba422238053b33ce4e1820ef68a", 5:55:08 PM: "0x036acc2f4036be7eb1f8649acf8d356797422a447b86901050eb75eb42336b62", 5:55:08 PM: "0xbda6af8e3ddba44ddc7a49bde005b47c2824991b7f0c3333fdbf57030d9068ef", 5:55:08 PM: ], 5:55:08 PM: "transactionsRoot": "0x6243406b2d47747e4ba236916854fdb31e346f207296734ed8f28a40561dc1c0", 5:55:08 PM: "uncles": Array [], 5:55:08 PM: }

eabaid avatar Jan 06 '18 23:01 eabaid

I was able to make it work using web3 version 0.20.x, in case it helps someone, as a workaround I created a babel preset for react-native to use it out of the box.

https://github.com/agrcrobles/babel-preset-react-native-web3

Edit: Looks like this issue it's cloned from https://github.com/ethereum/web3.js/issues/1108 and https://github.com/ethereum/web3.js/issues/576

agrcrobles avatar Jan 16 '18 20:01 agrcrobles