How can I solve Webpack error using MQTT.js with ReactJS - 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

Related

Vite esbuild failing for old Non-ESM package

I'm trying to use glsl-transpiler(https://github.com/stackgl/glsl-transpiler) with my vite project but am running into issues with esbuild, which outputs
[ERROR] [plugin vite:dep-pre-bundle] Failed to resolve entry for package "escaper". The package may have incorrect main/module/exports specified in its package.json.
Adding optimizeDeps: {exclude:['glsl-transpiler']} to defineConfig in vite.config.js allows the app to build, but I get a require is not defined error when I try to use the package due to the https://github.com/stackgl/glsl-transpiler/blob/master/index.js ll:8.
Does anyone know how I can successfully use this old package and its old dependencies in my project?

WalletConnect issue importing 'crypto'

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.

Module parse failed with custom npm package

I'm new to React and new to npm module publishing, there is a beginning for all things!
I have created a react module.
I have created a react app (using create-react-app) and i have imported the module directly (not via a npm install).
On this case, i got no errors. The app is compiling without problems.
However, when i'm trying to use my module by importing it from npm (after it's get published), it fails.
To get this issue, i have created another react app (always by using create-react-app) then i have installed my package using npm i --save my-module-name.
I'm importing it commonly by using import { SomeComponent } from 'my-module-name' instead of importing it directly.
And i got the following error
Module parse failed: Unexpected token (59:37)
You may need an appropriate loader to handle this file type.
| }
|
| generateRecoverActionsContainers = () => {
|
After some searchs, it seems to come from the Webpack configuration. But, during my searchs, i saw that people recommand to not touch the react Webpack default configuration.
From there, i'm a bit lost on how to fix that and also, why it was compiling when the module was imported directly and not compiling when it was importing from node_modules... Should i had a webpack configuration in my npm module?
Thanks in advance for your precious help!
When you publish a module on npm, you should publish compiled code. Webpack doesn't compile node_modules

Could not load module 'react/addons'

I have Jest+Enzyme setup to test my React app and I use brunch to build the app. When I run brunch, I got following error
Processing of node_modules/enzyme/build/react-compat.js failed. Error: Could not load module 'react/addons' from '/home/Project/node_modules/enzyme/build'. Possible solution: runnpm install.
But I have no trouble running Jest. I use yarn and yarn install does nothing as I have all packages installed. What is this missing react/addons package?
Solved the issue by telling brunch to ignore my test files
conventions: {
ignored: [
'**/*.test.js'
]
}

NativeScript with ApolloClient issue

I created a standard app with tns create project --ng which ran fine (and adding platform android)
I also wanted to use graphQL so added apollo-client and graphql tag:
npm install apollo-client --save
npm install angular2-apollo --save
npm install graphql-tag --save
So now when I build with tns build android the error I get is...
node_modules/#types/isomorphic-fetch/index.d.ts(7,5): error TS2300:
Duplicate identifier '"audio"'.
which clashes with (from what I can see)
node_modules/tns-core-modules/declarations.d.ts(25,5): error TS2300:
Duplicate identifier '"audio"'.
isomorphic-fetch is added when apollo-client was added. Not too sure about the typings issue but would be good to know how to get around this.
EDIT:
looking at the guide
I have the 2 items set to false in the tsconfig.json which seem to continue despite of the error...
"noEmitHelpers": false,
"noEmitOnError": false
But I would like to know how to fix this properly.
I ran into the same issue but worked around it in a project with the following dependencies:
nativescript#2.5.1
typescript#2.2.1 (bundles isomorphic-fetch typings in dom)
apollo-client#1.0.0-rc.5
This requires changes to tsconfig.json and references.d.ts described in my response to Github nativescript-dev-typescript issue 19.
While this setup works in the browser with Angular CLI and builds for NativeScript using tns android without relaxing "noEmitOnError": true in tsconfig.json, I then had to debug a runtime exception with an apollo-client call to redux which attempts to call node.js code leading to the following error:
ReferenceError: process is not defined
…
/tns_modules/redux/lib/index.js', line: 38, column 4
This was worked-raound by adding global.process = { env: {} }; as the first line in main.ts, as described in NativeScript Github issue 2937, which StackOverflow won't allow me to link to due to my lack of reputation points.
Well I see tns-core-modules/declarations.d.ts is imported with tns-core-modules/tns-core-modules.base.d.ts so actually Nativescript should provide optionally one more definition where they dont include the request definitions in the base file

Resources