Plugin ‘BarcodeScanner’ not found, or is not a CDVPlugin.

While testing a Phonegap app on an iOS device with the BarcodeScanner plugin, I encountered the following error:

ERROR: Plugin 'BarcodeScanner' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.

I could not find any solutions that worked for me online, so I had to experiment and here’s what I found to work for me.

After creating the Phonegap app, downloading the plugin and using your own code or this demo:

  1. Copy barcodescanner.js to <your_app>/www/js
  2. Wrap all the code with:
    cordova.define("com.phonegap.plugins.barcodescanner.BarcodeScanner", function(require, exports, module) {
    /* barcodescanner.js goes here */
    });
  3. Adjust the path to barcodescanner.js in the script tag in index.html
    src="js/barcodescanner.js"
  4. Build the app in terminal:
    phonegap build ios
  5. Deploy app to iOS device on Xcode

Let me know if this works for you or if you found other solutions. Curiously, building the app more than once in terminal makes this not work :/