Javascript – “Require” exception in Cordova/phonegap project

androidcordovajavascriptnode.js

I am trying to build an hybrid mobile app using Phonegap/Cordova in Android platform. And I have succeeded in that too 🙂 The app works as desired in Ripple emulator in my Chrome browser. I am not using Eclipe ADT or Android Studio as the emulator does not load quick.

I have used onDeviceReady event and Navigator plugin for vibrate and alerts. All these works fine as expected.

When the Developer Console is active in Chrome, the JavaScript breaks in cordova.js with the following error

Uncaught ReferenceError: require is not defined 

in the following line. The same happens for notification.js file too. When I press continue in debugger, everything works again as expected apart from that error.

var cordova_events = require('./src/events'),

Order of the java script files are as given below.

<script src="js/cordova.js"></script>
<script src="js/notification.js"></script>               
<script src="js/vibration.js"></script> 

I believe I am not referencing the correct cordova.js file and all other dependencies. But I also wonder how come does the app works fine in Ripple when the developer console is not active.

I even tried to remove cordova.js file as Ripple automatically includes it as per phonegap deviceready event – ripple emulator

I have the download copies of phonegap and cordova from their respective sites. I have installed Node.js and have installed the packages too.

My questions here are:

  • Where to find the correct cordova.js file? Which is the correct version to be included in my html files project?
  • What's the user of Node.js over here?
  • Does it designed to automatically include the scripts based on Node.js? If so what I am missing?

I confess that the concept and usage of node.js looks like a rocket science for me.

Best Answer

Dont use the plugins *.js files out of the plugin source.

Dont add plugins *.js files as tags into your html
(Cordova loads them on its own based on cordova_plugins.js )

The specific error 'require is not defined' comes from missing cordova define in the plugins.js

cordova.define("org.apache.cordova.file.DirectoryEntry", function(require, exports, module) {

});

To avoid all of this trouble:

Use cordova command line interface to setup platforms and plugins. It manages all the native and javascript source files and puts them together in the proper way.

Related Topic