Question

I'm using worklight and zxing for my qr reader mobile app project. My problem is, i got this error after I run this app in my google chrome.

Uncaught ReferenceError: require is not defined barcodescanner.js:10

Error line 10 in barcodescanner.js

var exec = require("cordova/exec");

Even I included cordova file into index.html, the error message still occuring.

<!DOCTYPE HTML>
<html>
        <head>
            <meta charset="UTF-8">
            <title>index</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
            <link rel="shortcut icon" href="images/favicon.png">
            <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
            <link rel="stylesheet" href="css/jquery.mobile-1.3.1.css">
            <link rel="stylesheet" href="css/custom.css">
            <link rel="stylesheet" href="css/footer.css">
            <!-- require.js:  data-main attribute tells require.js to load
             js/main.js after require.js loads. -->

             <script>window.$ = window.jQuery = WLJQ;</script>
             <script src="js/vendor/phoneGap/cordova-1.6.0.js"></script>
             <script src="js/vendor/phoneGap/barcodescanner.js"></script>
             <script data-main="js/main" src="js/vendor/require/require.js"></script>

        </head>
        <body>

        </body>
</html>

Check my repo here: https://github.com/datomnurdin/worklight-mobile

Was it helpful?

Solution

The barcode scanner code will not work outside the supported environments (iOS devices and Android devices, according to the plugin page).

Wrap environment specific code like the one inside barcodescanner.js in environment check tags. For example:

if (WL.Client.getEnvironment() === "android") {
   //barcodescanner.js and other code that should only run on android
}

Also, try replacing in barcodescanner.js:

var exec = require("cordova/exec")

with:

var exec = cordova.exec

or:

Try an older version of the plugin like 0.7.2, notice the use of cordova.define to provide the require function, instead of expecting the require global function to magically exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top