-
Npm or Node Package Manager is the default package manager for Node.js. It works as:
- An online repository called as npm registry used for the open-source Node.js projects.npm registry is a large database with around half a million packages. Developers can download from and publish packages to the npm registry.
- Npm is also a command-line utility for interacting with an online repository that helps in package installation, version management, and dependency management.
Few of the important npm commands are:
Any package can be installed by running a simple command
npm install <name of the package>
This will install the module under the path,
./node_modules/
. Once installed the module could be used just like they were built-ins. Dependency management could also be done with npm. Our node project will have apackage.json
which will have all the dependencies needed for our project. If we performnpm install
from project root all the dependencies listed in thepackage.json
file will be installed.Other Commands :
npm init
Herepackage.json
file is created that is where all dependencies are included.npm updat <package_name>
where the specific package is updated with new featurenpm uninstal <package_name>
where the specific package is uninstalled. It’s then removed from thenode_modules
foldernpm list
is used to list all the packages installednpm help
is the built-in help command.To get help for a particular command, usenpm <command> -h
-
Apache Cordova is an open-source mobile development framework. It allows you to use standard web technologies - HTML5, CSS3, and JavaScript for cross-platform development. Applications execute within wrappers targeted to each platform, and rely on standards-compliant API bindings to access each device's capabilities such as sensors, data, network status, etc.
-
Cordova provides several features some of them are as follows:
- Command line interface used for starting project, building processes for different platforms, installing plugins.
- Cordova provides plugins that are used for implementing native mobile functions.
- Cordova provides set of component which are used for creating base of the app so that developer spend more time to implement own logic.
- Cordova is licensed under the Apache License, version 2.0.
-
The limitations of Cordova are:
- The Hybrid applications are slower than native ones so it is not a great idea to use Cordova for large applications that need lots of data and functionality.
- Testing and optimizing is time consuming because we need to cover different types of operating systems.
- The Cross-browser compatibility can create lots of issues. Most of the time we are developing apps for different platforms, so the testing and optimization can take a lot of time and effort as we need to cover a large number of devices and OS.
- Some plugins also have compatibility issues with the varying devices and platforms. There are also native APIs that are not yet supported by Cordova.
-
The config.xml file provides facility for changing the configuration of the app.
It is a platform-agnostic xml file that consists of all the necessary information required by the Cordova to convert the code in the www folder to the platform-specific installer. This file controls the Cordova behavior based on the W3C's packaged web apps. It also specifies the metadata of the application. It is a default file that is automatically created when creating any Cordova app.
This file can be edited either manually or using specific commands of Command Line Interface (CLI). We can add two configurations to this file, i.e., global and platform-specific.
The global configuration specifies the configuration that is common to all the devices, whereas the platform-specific configuration is specific to the platform.
-
The below table defines the elements used in the config.xml file:
Elements Description widget It defines the reverse domain value of an app that should be specified when creating the app. name It defines the name of an app. description It represents the description of an app. author It represents the contact information that can be shown within app-store listings. content It represents the starting page of an app at the top-level web assets directory. The default value is index.html that appears at the top-level www directory. plugin It is an additional feature for enhancing the capabilities of Cordova. It can be defined as a package of code that helps to communicate with the native platforms. access It is used to control access for a specific network domain. It has the default origin value *, which shows that the access is opened to any domain. engine It specifies the details about the platform, which is restored during the implementation. allow-intent It is used for enabling the specific URLs to ask the app to open. hook It represents your custom script that is called by Cordova when a particular action occurs. It is useful for extending the default Cordova functionality. platform It represents a platform where we build our application. resource-file It installs the resource file into the system.
-
Cordova events are a way to catch any event that takes place during the process of running your app. It is an event system that handles events originating from the device such as touch, orientation, and motion.
The following table shows the available events that can be used in Cordova projects.
Event Detail deviceReady This event is triggered once Cordova is fully loaded. This helps to ensure that no Cordova functions are called before everything is loaded. pause This event is triggered when the app is put into background. resume This event is triggered when the app is returned from background. backbutton This event is triggered when the back button is pressed. menubutton This event is triggered when the menu button is pressed. searchbutton This event is triggered when the Android search button is pressed. startcallbutton This event is triggered when the start call button is pressed. endcallbutton This event is triggered when the end call button is pressed. volumedownbutton This event is triggered when the volume down button is pressed. volumeupbutton This event is triggered when the volume up button is pressed.
-
When a user clicked on the back button, backbutton event is fired. Register an event listener for backbutton event, if you want to override the behaviour of default back-button.
document.addEventListener('backbutton', onBackKeyDown, false); function onBackKeyDown() { // Handle the back button }
-
Cordova CLI is the command line tool to build, deploy and manage Cordova-based applications.
Please find the Cordova CLI related questions below :
- Which CLI command, prepares then packages web applications into native Cordova applications ?
cordova prepare [<platform> [..]]
- Which CLI command, creates a Cordova project and associated project folders and files ?
cordova create path [id [name]]
Value Description path Directory which should not already exist. Cordova will create this directory. id Reverse domain-style identifier that maps to id attribute of widget element in config.xml. name Application's display title that maps name element in config.xml file. - Which CLI command, allowing you to add, remove, update and list platforms?
# Add platform to the project and remember that in config.xml & package.json. cordova platform add [platform] ## example cordova platform add android cordova platform add android ios # remove platform cordova platform remove [platform] cordova platform rm [platform] ## example cordova platform rm android # List all installed and available platforms cordova platform list
- Which CLI command, add, remove, update and list cordova plugin in the project?
# Add plugin to the project and remember that in config.xml & package.json. cordova platform add cordova-plugin-camera # remove plugin cordova plugin remove cordova-plugin-camera # list all installed and available plugin cordova plugin list
-
Below is the directory structure for any cordova project genearated with Cordova CLI.
``` myapp/ |-- config.xml |-- merges/ | | |-- android/ | | |-- windows/ | | |-- ios/ |-- www/ |-- platforms/ | |-- android/ | |-- windows/ | |-- ios/ |-- plugins/ |--cordova-plugin-camera/ ```
-
Cordova Hooks represent special scripts which could be added by application and plugin developers or even by your own build system to customize cordova commands.
Cordova hooks allow you to perform special activities around cordova commands. For example, you may have a custom tool that checks for code formatting in your javascript file. And, you would like to run this tool before every build. In such a case, you could use a
before_build
hook and instruct the cordova run time to run the custom tool to be invoked before every build.Cordova supports the following hook types:
Hook Type Description before_platform_add
,after_platform_add
To be executed before and after adding a platform. before_platform_rm
,after_platform_rm
To be executed before and after removing a platform. before_platform_ls
,after_platform_ls
To be executed before and after listing the installed and available platforms. before_prepare
,after_prepare
To be executed before and after preparing your application. before_compile
,after_compile
To be executed before and after compiling your application. before_deploy
To be executed before deploying your application. before_build
,after_build
To be executed before and after building your application. before_run
,after_run
To be executed before and after running your application. before_serve
,after_serve
To be executed before and after serving your application. before_plugin_add
,after_plugin_add
To be executed before and after adding a plugin. before_plugin_rm
,after_plugin_rm
To be executed before and after removing a plugin. before_plugin_install
,after_plugin_install
To be executed before and after installing a plugin. before_plugin_uninstall
To be executed before uninstalling a plugin. Ways to define hooks
Hooks could be defined in project's
config.xml
using<hook>
elements, for example:``` <hook type="before_build" src="scripts/appBeforeBuild.bat" /> <hook type="before_build" src="scripts/appBeforeBuild.js" /> <hook type="before_plugin_install" src="scripts/appBeforePluginInstall.js" /> <platform name="windows"> <hook type="before_build" src="scripts/windows/appWinBeforeBuild.bat" /> <hook type="before_build" src="scripts/windows/appWinBeforeBuild.js" /> <hook type="before_plugin_install" src="scripts/windows/appWinBeforePluginInstall.js" /> ... </platform> <platform name="android"> <hook type="before_build" src="scripts/android/appAndroidBeforeBuild.bat" /> <hook type="before_build" src="scripts/android/appAndroidBeforeBuild.js" /> <hook type="before_plugin_install" src="scripts/android/appAndroidBeforePluginInstall.js" /> ... </platform> ```