简体   繁体   English

Cordova / phonegap项目中的“要求”例外

[英]“Require” exception in Cordova/phonegap project

I am trying to build an hybrid mobile app using Phonegap/Cordova in Android platform. 我正在尝试使用Android平台中的Phonegap / Cordova构建混合移动应用程序。 And I have succeeded in that too :) The app works as desired in Ripple emulator in my Chrome browser. 而且我也成功了:)该应用程序在我的Chrome浏览器中按照Ripple模拟器的需要运行。 I am not using Eclipe ADT or Android Studio as the emulator does not load quick. 我没有使用Eclipe ADT或Android Studio,因为模拟器无法快速加载。

I have used onDeviceReady event and Navigator plugin for vibrate and alerts. 我使用onDeviceReady事件和Navigator插件进行振动和警报。 All these works fine as expected. 所有这些都按预期正常工作。

When the Developer Console is active in Chrome, the JavaScript breaks in cordova.js with the following error 当开发者控制台在Chrome中处于活动状态时,JavaScript会在cordova.js中断,并显示以下错误

Uncaught ReferenceError: require is not defined 

in the following line. 在以下行中。 The same happens for notification.js file too. 对于notification.js文件也是如此。 When I press continue in debugger, everything works again as expected apart from that error. 当我在调试器中按下continue时,除了该错误之外,所有内容都会按预期再次运行。

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

Order of the java script files are as given below. java脚本文件的顺序如下所示。

<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. 我相信我没有引用正确的cordova.js文件和所有其他依赖项。 But I also wonder how come does the app works fine in Ripple when the developer console is not active. 但我也想知道当开发者控制台不活动时,应用程序如何在Ripple中正常工作。

I even tried to remove cordova.js file as Ripple automatically includes it as per phonegap deviceready event - ripple emulator 我甚至尝试删除cordova.js文件,因为Ripple会根据phonegap deviceready事件自动包含它- 波纹模拟器

I have the download copies of phonegap and cordova from their respective sites. 我有各自网站的phonegap和cordova的下载副本。 I have installed Node.js and have installed the packages too. 我已经安装了Node.js并安装了软件包。

My questions here are: 我的问题是:

  • Where to find the correct cordova.js file? 在哪里可以找到正确的cordova.js文件? Which is the correct version to be included in my html files project? 哪个是我的html文件项目中包含的正确版本?
  • What's the user of Node.js over here? Node.js的用户在这里是什么?
  • Does it designed to automatically include the scripts based on Node.js? 它是否设计为自动包含基于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. 我承认node.js的概念和用法对我来说看起来像火箭科学。

Dont use the plugins *.js files out of the plugin source. 不要使用插件来源的插件* .js文件。

Dont add plugins *.js files as tags into your html 不要将插件* .js文件作为标签添加到您的html中
(Cordova loads them on its own based on cordova_plugins.js ) (Cordova基于cordova_plugins.js自行加载它们)

The specific error 'require is not defined' comes from missing cordova define in the plugins.js 特定错误'require not defined'来自plugins.js中缺少的cordova定义

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. 使用cordova命令行界面来设置平台和插件。 It manages all the native and javascript source files and puts them together in the proper way. 它管理所有本机和javascript源文件,并以正确的方式将它们组合在一起。

Cordova and Phonegap are pretty much the same thing, you shouldn't need to download both. Cordova和Phonegap几乎是一回事,你不需要同时下载它们。 Cordova is the open source project that helps you publish your HTML5 app to multiple different mobile OSes. Cordova是一个开源项目,可帮助您将HTML5应用程序发布到多个不同的移动操作系统。 Phonegap uses Cordova to do that, but also adds some extra features, mostly just being able to build in the cloud instead of on your workstation. Phonegap使用Cordova来做到这一点,但也增加了一些额外的功能,主要是只能在云中而不是在工作站上构建。

Node.js is used by Cordova for a lot of the building steps. Cordova使用Node.js进行许多构建步骤。 Since Cordova works on OSX and Windows machines, we needed a way to write build and package scripts that would work on both operating systems - node.js provides this. 由于Cordova适用于OSX和Windows机器,我们需要一种编写可在两个操作系统上运行的构建和包脚本的方法 - node.js提供了这种方法。 When you build an application with Cordova you shouldn't really use node.js at all, unless you are also building a complementary backend system. 当您使用Cordova构建应用程序时,您根本不应该使用node.js,除非您还构建了一个补充后端系统。

Starting with Cordova 3.x, there is a cordova command line tool that can greatly help you create the application. 从Cordova 3.x开始,有一个cordova命令行工具可以极大地帮助您创建应用程序。 It takes care of copying the right cordova.js and cordova-android.jar files. 它负责复制正确的cordova.js和cordova-android.jar文件。 You can read about it here http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface 你可以在这里阅读http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface

Once you get set up you might want to read my other answer that clarifies some of the use cases of cordova tool: Should a phonegap plugin be declared in the config.xml file? 设置完成后,您可能希望阅读我的其他答案,其中阐明了cordova工具的一些用例: 是否应在config.xml文件中声明phonegap插件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM