WalletConnect issue importing 'crypto' - reactjs

I'm using wallet connect, but when I try to do the connect it gives me this error:
Module not found: Error: Can't resolve 'crypto'
And then it gives me this suggestions:
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 }
I tryed but didn't work, somebody can help me?
The code of the connection is the same as how the website explain.
(I'm using React)
here more: https://docs.walletconnect.com/quick-start/dapps/node

I have encountered this issue multiple times. This is because of webpack version 5 which introduces breaking changes. There are 2 solutions to this:-
Either follow what is written in this article (make sure to install and add a fallback for crypto in the same manner as mentioned for others in this article).
Downgrade react-scripts from 5 to 4.0.3 and use with an LTS version of NodeJS.
Option 2 is easier and I would prefer it. In case you do too, follow these steps:
Run npm uninstall react-scripts.
Run npm install react-scripts#4.0.3.
Run npm run start to check if this works. If there is any error, delete the node_modules and then reinstall using npm install that should sort things out.

Related

React App Compile Errors After Installing Express and Cors

I am working on a React app that focuses on getting an mp3 file from a server but I am getting a CORS error. While trying to follow a tip to enable CORS I started off by installing express and cors via npm ('npm install express' and 'npm install cors'). However I am now getting 30 compile errors. Here is one such error message:
ERROR in ./node_modules/body-parser/lib/read.js 24:11-26
Module not found: Error: Can't resolve 'zlib' in '/home/brian/Projects/music-player/node_modules/body-parser/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: { "zlib": require.resolve("browserify-zlib") }'
- install 'browserify-zlib'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "zlib": false }
How do I solve this issue and what are these webpacks and polyfills? Thanks in advance for helping.
Unfortunately, that's not a lot of information on how your app is set up. I guess you are trying to install express (to set up your server) inside your frontend app. If that is the case I would suggest you uninstall it from react app and install it in another directory.
However if the issue still persists this is what works for me. On my side, there was an issue with npm cache.
try to clean your npm cache: npm cache clean --force
If that does not work:
delete your node modules
delete package-lock.json file
use npm install to get the updated node modules and new package-lock.json file
Polyfill is used to implement features on old browsers that do not support a specific feature (for example array.includes() is not supported by Internet Explorer 11)
Webpacks is a module bundler. Its purpose is to compile JavaScript modules and it basically takes your project dependencies (package.json) and generates a dependency graph.
Now regarding the CORS issue. Do you own the server you are trying to retrieve mp3 data? Then you should allow cross origin from that server, setting appropriate headers in the server response: Access-Control-Allow-Origin: *
If you do not own, or you do not have access to the server, you can set up your own server to handle api calls and retrieve data from another server without any CORS issue. You can do it with Express and then request the resource via your server, and send the response back to your frontend.
Just note that you cannot fix the CORS issue directly from the frontend!

Include polyfills in webpack > 5 in CRA

I'm trying to use an NPM package called "libsodium-wrappers" inside a browser with React using Create React App, but getting the following error:
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: { "path": require.resolve("path-browserify") }'
- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "path": false }
After searching the web about this, I found that I may downgrade the "react-scripts" to version 4, but I really don't want to do this, as I will need to reinstall everything and this may break things.
Are there any working solutions to this besides the above one?
Thanks
It may be related to a component library you may be using. I came across you question while attempting to address this very same issue. I'm using react-bootstrap-table2 in a project that is causing this issue for me. I believe I may have resolved it by installing the events package (specifically for react-bootstrap-table2's needs. In this case maybe try this:
yarn add path -D
Hopefully in this case that may "bump" the needle in a positive direction for you.
Do yarn add util (alternatively, npm install util). It worked for me.

How can I solve Webpack error using MQTT.js with ReactJS

I want to achieve that by clicking on a button in a ReactJS project a MQTTjs handler sends/publishes a message to a broker. MQTT functionality is already tested without React. Im running my React Project on macOS (v11.6.2) with WebStorm (v2021.3).
For solving that I followed the instructions from MQTTjs on Github and NPM. Namely:
npm install -g webpack
npm install mqtt
cd node_modules/mqtt
npm install .
webpack mqtt.js --output-library mqtt
While npm install . throws no errors webpack mqtt.js --output-library mqtt does:
Module not found: Error: Can't resolve 'mqtt.js' in '/Users/Marco/JetBrains/WebStorm/WG/kitchen_control_view/node_modules/mqtt'
Did you mean './mqtt.js'?
So I tried it again with adding ./: webpack ./mqtt.js --output-library mqtt. This time I don't get any errors but warnings:
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
I thought 'No errors?! Let's give it a try'.
Edit: I could get rid of the warning with ./node_modules/.bin/webpack ./mqtt.js --mode=development --output-library mqtt
So in one of my JSX files I import mqtt as stated at Github or NPM.
import mqtt from "mqtt"
function App() {
return (
<div>
<p>Test</p>
<div>
)
}
I didn't use the mqtt package already, just imported it. And this throws me an error in console:
and this error in web browser:
Compiled with problems:
ERROR in ./node_modules/mqtt/lib/connect/index.js 7:12-26
Module not found: Error: Can't resolve 'url' in '/Users/Marco/JetBrains/WebStorm/WG/kitchen_control_view/node_modules/mqtt/lib/connect'
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: { "url": require.resolve("url/") }'
- install 'url'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "url": false }
Here are the versions of mqtt and react from package.json:
"mqtt": "^4.3.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
I then installed buffer with npm install buffer. But then React throws a lot of errors in the browser. I just picked the first one. The others are quite similar and point to the same path /Users/Marco/JetBrains/WebStorm/WG/kitchen_control_view/node_modules/mqtt/node_modules/[PACKAGE_NAME]
Compiled with problems:
ERROR in ./node_modules/mqtt/node_modules/debug/src/browser.js
Module build failed (from ./node_modules/source-map-loader/dist/cjs.js):
Error: ENOENT: no such file or directory, open '/Users/Marco/JetBrains/WebStorm/WG/kitchen_control_view/node_modules/mqtt/node_modules/debug/src/browser.js'
The console error changed to:
I appreciate any help and please let me know if I forgot some important information.
Best,
Marco
I too faced the same issue, after lot of research I was able to make it work. So I decided to publish a build version of mqtt#4.3.7 here . So instead of
import mqtt from "mqtt";
import the precompiled-mqtt package
import mqtt from "precompiled-mqtt";
And everything will be same the official mqtt.js

Getting the error "Module not found: Error: Can't resolve 'querystring'", how do I fix this?

After installing react-instagram-embed package I got this error and tried all possible solution but still not working!!! Anyone knows solution for this??
Module not found: Error: Can't resolve 'querystring' in
'C:\Users\vytck\Desktop\ibm\node_modules\finnhub\dist'
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: { "querystring": require.resolve("querystring-es3") }'
install 'querystring-es3' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: {
"querystring": false }
querystring is deprecated since at least since last Node LTS version 14.
So if you want to keep your current version of react-instagram-embed
install querystring-es3
yarn add querystring-es3 or npm i querystring-es3
in your webpack.config.js configuration file update or add the resolve entry to look like this
resolve: {
fallback: { "querystring": require.resolve("querystring-es3") }
}

How to use messageformat from npm

I installed messageformat from npm for use in my angular-boostrap project in conjunction with angular-translate.
If I simply add messageformat v0.3.1 via CDN, everything works as expected. But I would like to use messageformat installed from npm. The trouble is, I have no idea how. None of the scripts in the messageformat npm package looks anything like the one on CDN. I am wondering if I need to build it or something? But then again, the documentation on npm for messageformat does not mention anything apart from "npm install...".
What am I missing?
[Edit]
There is both a bin and a lib folder in the project. Both contain messageformat.js, but none of them are working, and they look nothing like the one from CDN. If I include node_modules/messageformat/lib/messageformat.js I get a "module is not defined" error. If I include node_modules/messageformat/bin/messageformat.js I get "invalid or unexpected token" ... Therefore I suspect there is a step missing to get a script like the one from CDN
you need to add the files instead of cdn :
<script src="path/to/node_module/messageformat/messageformat.js"></script>
It turns out that version 0.3.1 of the npm package did not include a UMD version of the script, but since then version 1.0.0 is out, and a script ready for use in browsers now ships with the npm package.
https://github.com/messageformat/messageformat.js/issues/157

Resources