Search code examples
javascriptvisual-studio-2015cordova-pluginsmqttvisual-studio-cordova

mqtt cordova visualstudio2015 : Uncaught ReferenceError: require is not defined


MQTT Plugin : https://www.npmjs.com/package/mqtt I installed MQTT Plugin using

npm install mqtt --save

And, also i did below step Browserify

1.npm install -g browserify // install browserify
2.cd node_modules/mqtt
3.npm install . // install dev dependencies
4.browserify mqtt.js -s mqtt > browserMqtt.js // require mqtt in your client-side app

Webpack

1.npm install -g webpack // install webpack
2.cd node_modules/mqtt
3.npm install . // install dev dependencies
4.webpack mqtt.js ./browserMqtt.js --output-library mqtt

And i added like this in my .html file

    <script src="../browserMqtt.js"></script>
    <script src="scripts/paho-mqttws31.js"></script>
    <script src="scripts/mqtt.js"></script>

and i also added in my .js file

const mqtt = require("mqtt");

and i have got errors.

paho-mqttws31.js:2268 Uncaught ReferenceError: module is not defined at paho-mqttws31.js:2268 (anonymous) @ paho-mqttws31.js:2268 =>if(module && module.exports) module.exports=Paho;

mqtt.js:1 Uncaught ReferenceError: require is not defined at mqtt.js:1 =>var Paho = require('cordova-plugin-mqtt-pahojs.paho_mqttws31');

these two error are occurred and '=>' mark is pointed to a place which is occurring error.

So, i have deleted this two

then i got one error that

Uncaught ReferenceError: require is not defined

and linked as error occurring position

And i got one more error :

Failed to load resource: the server responded with a status of 404 (Not Found) 4400/favicon.ico

I'm getting favicon.ico error

I'm getting favicon.ico error

I have searched and been applying some references above. However, it didn't work at all.


Solution

  • I fixed this problem. In your config.xml, add your plugin like this

    <plugin name="cordova-plugin-mqtt" version="0.3.8" /> <plugin name="cordova-plugin-device" version="2.0.1" /> 
    <plugin name="cordova-plugin-mqtt-pahojs" version="1.5.2" />  
    

    And if you are using visual studio, you can ignore favicon error.