-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Haste error messages (React-Native) #268
Comments
What is Haste? |
@ricmoo Haste is an RN thing which basically means the module is not found. |
There should be no For TypeScript, the library should be imported with:
But I don't see how that could be the problem. I've never used RN, but can you provide a very simple project which replicates the issue? |
https://github.com/strangepowers/test_ethers_react_native this is the commit that doesn't work: https://github.com/strangepowers/test_ethers_react_native/commit/94fc5527ae5da099b1c9417133e16775424221e6 I suspect it may have something to do with these: facebook/react-native#14796 so I tried experimenting with https://www.npmjs.com/package/rn-nodeify using this project as an example: https://github.com/mvayngrib/react-native-crypto , but no luck so far. |
You can inject it via
At least it worked fine for similar issue with web3.js |
I've been trying to fix this. It seems like the react-native environment overwrites
It seems like the runtime does very strange things. I'm trying to find a work around. |
I have found a solution. It is HACKY. It is VERY hacky! Overview of the problem, The module system for React-Native hijacks any variable with the name require, unless immediately in the scope it is replaced with its the Haste So, easy work around, assign Well, yes, that would be. Luckily we already have something that has done that for us. To use
The minified version of ethers.js has already replaced all instances of the function named "require" with other, less-human-readable (more space-saving) variables names like Le sigh. |
Making ethers.js Thanks for your work on the project! P.S. |
We actually just finished un-modularizing it. Making things modular makes the thing as a whole larger, in general. What modularization does it make it smaller for people who only require a subset of features, but if you need it all anyways, it is larger. Modularization also makes testing and updating dependencies a nightmare to do with explicit versions, which are important for security. We solve this by keeping pathes stable within a release. For example, if you only need keccak256, Within a major version, the paths of functionality will not change. The reason |
Thanks for the update, @ricmoo! You're doing good with possibly the most lightweight, full and stable-functioning Ethereum client, keep it going! However it took me almost a half of the day to refactor from v3 to v4 of the library (yet not completely), because many things were changed (also, the migration guide is not complete); v3 had a bug with wrong gas price estimation (it couldn't estimate gas for contracts where |
Thanks! Pleas let me know any missing parts of the migration guide too. I need to add a mapping of file paths from v3 to v4 too. The gas limit thing is driving me insane. In v3, I hard-coded 1.5 million gas, which had its own problems. In v4, I use estimate gas, but it seems like if the contract calls an external contract, estimate gas throws its hands in the air and just returns a number that is basically infinite (larger than the block gas limit). I need to figure out some reasonable threashold, that over that, it sets it to that max, or something... But the. It interferes where the gas limit accurately detects the call would fail. :s The from should be included in estimate gas, but I’ll look into that and verify that too. |
Oh! Interesting. I see the bug you mean! I’ll fix that ASAP. :) |
Oh! (again) that’s version 3. I will double check all the code paths in v4 to make sure from is properly propagated. Basically, I’ll look into this shortly. :) |
And how to import the wordlists. I try to:
Get the error: Cannot read property 'utils' of undefined. P.S. I run it on react-native. |
Heya! So, I think I've solved the RN issues... Can you upgrade to // Import the required shims
import 'ethers/dist/shims.js';
// Import the ethers library
import { ethers } from 'ethers'; Notes: https://docs.ethers.io/ethers.js/html/cookbook-react.html |
@zysam I don't think that last push will fix your issue though, I will look into that tomorrow. |
@zysam To fix your issue, you need to use the React-Native import syntax (the require method is for browsers, which exposes a global object). import { ethers } from 'ethers';
import { zh_cn } from 'ethers/wordlists';
let mnemonic = ethers.Wallet.createRandom({ locale: zh_cn }).mnemonic; Let me know if you still have issues with that. :) |
This should be fixed now entirely. Please try out adding the shims from Thanks! :) |
Thank you. It works! I try to do this: import 'ethers/dist/shims'
// import 'ethers/wordlists/lang-zh'
import {ethers} from 'ethers'
import * as wordlists from 'ethers/wordlists'
getRandomMnemonic(n = 16, lang = 'en') {
// Chose the length of your mnemonic:
// - 16 bytes => 12 words (* this example)
// - 20 bytes => 15 words
// - 24 bytes => 18 words
// - 28 bytes => 21 words
// - 32 bytes => 24 words
const bytes = this.ethers.utils.randomBytes(n)
// Select the language:
// - en, es, fr, ja, ko, it, zh_ch, zh_tw
// const wordlists = this.ethers.utils.Wordlist
const language = wordlists[lang]
console.log('language:', lang, language)
const randomMnemonic = this.ethers.utils.HDNode.entropyToMnemonic(bytes, language)
return randomMnemonic
} |
Awesome!! Let me know if there are any more problems. :) Thanks! |
Is there a reason you don't instead wrap XmlHttpRequest in something like this? if (window && window.XmlHttpRequest) export = window.XmlHttpRequest
else export = require('XmlHttpRequest') Note, the above code snippet is meant to get an idea across, it probably isn't valid or correct. The current solution is non-obvious, I only figured it out after a bunch of troubleshooting. The proposed code snippet should (I think) "just work" in either Node or Browser without the user doing anything. |
In which file? Any file processed by Browserify needs the file replaced using the shims instead, otherwise the I would love to find a better solution though. My plan is to make the |
Looks like this solution doesn't work with webpack, which is hard-bundled into Angular. 😢 |
When running this code in react-native
import ethers from 'ethers'
I get this error message:
Module
buffer
does not exist in the Haste module map.To fix it I had to revert to 3.0.27
EDIT:
full error message:
error: bundling failed: Error: Unable to resolve module
buffer
from./node_modules/ethers/dist/ethers.js
: Modulebuffer
does not exist in the Haste module mapThis might be related to facebook/react-native#4968
To resolve try the following:
watchman watch-del-all
.node_modules
folder:rm -rf node_modules && npm install
.rm -rf /tmp/metro-bundler-cache-*
ornpm start -- --reset-cache
. 4. Remove haste cache:rm -rf /tmp/haste-map-react-native-packager-*
.at ModuleResolver.resolveDependency (/Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:167:1306)
at ResolutionRequest.resolveDependency (/Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:80:16)
at DependencyGraph.resolveDependency (/Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/node-haste/DependencyGraph.js:237:485)
at Object.resolve (/Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/lib/transformHelpers.js:116:25)
at dependencies.map.result (/Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/DeltaBundler/traverseDependencies.js:298:29)
at Array.map ()
at resolveDependencies (/Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/DeltaBundler/traverseDependencies.js:294:16)
at /Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/DeltaBundler/traverseDependencies.js:159:33
at Generator.next ()
at step (/Users/tim/Desktop/mobileWorkspace/albums/node_modules/metro/src/DeltaBundler/traverseDependencies.js:239:307)
The text was updated successfully, but these errors were encountered: