'Numerous errors when importing web3 into app.js
Attempt to import web3 into App.js gives me 9 errors
import React from "react";
import Web3 from "web3";
function App() {
return (
<div className="App">
<h1>TEST APP</h1>
</div>
);
}
export default App;
Compiled with problems:X
ERROR in ./node_modules/cipher-base/index.js 3:16-43
Module not found: Error: Can't resolve 'stream' in '/home/galich/Desktop/projects/mp-test/node_modules/cipher-base'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to: - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }' - install 'stream-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "stream": false }
ERROR in ./node_modules/eth-lib/lib/bytes.js 9:193-227
Module not found: Error: Can't resolve 'crypto' in '/home/galich/Desktop/projects/mp-test/node_modules/eth-lib/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to: - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - install 'crypto-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "crypto": false }
ERROR in ./node_modules/web3-eth-accounts/lib/index.js 31:74-91
Module not found: Error: Can't resolve 'crypto' in '/home/galich/Desktop/projects/mp-test/node_modules/web3-eth-accounts/lib'
etc.
Solution 1:[1]
1st Option:
Install NodePolyfillPlugin and react-app-rewired by using npm install react-app-rewired node-polyfill-webpack-plugin
or yarn add react-app-rewired node-polyfill-webpack-plugin
.
Now change "start": "react-scripts start"
to "start": "react-app-rewired start"
.
Create a file "config-overrides.js" and write the following code in it:
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
module.exports = function override(config, _env) {
config.plugins.push(
new NodePolyfillPlugin({
excludeAliases: ["console"],
})
);
return config;
};
Now do "npm start"
and Hurrah!!!
2nd Option:
Instead of importing like:
import Web3 from "web3";
Import like this:
import Web3 from "web3/dist/web3.min.js";
This will also solve your problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Farasat Ali |