Fix for Ionic 2 and TypeScript cannot find name cordova

You can access many Cordova plugins in Ionic 2 apps based on TypeScript with Ionic Native but there are also other Cordova plugins that are not supported by Ionic native .In this case all you have to do it to use the unsupported plugin directly but you need to have access to the cordova object .

Basically there are two options :

If the cordova plugin is supported by Ionic Native you can use the plugin by just importing it from ionic-native module .For example

Lets suppose we have added a plugin with

cordova plugin add cordova-plugin-inappbrowser

All you need to do to access it from your component or TypeScript class is

Import the plugin from 'ionic-native'

import { InAppBrowser } from 'ionic-native';

And then call any api

let browser = new InAppBrowser(url,'_system');

If the Cordova plugin is not supported ,in this case you need access to cordova object from TypeScript code

If you try you will get this error :

Cannot find name 'cordova'

So how to fix this error ?

First you need to install the typings module via npm with

npm install -g typings

Then use typings to install the TypeScript definitions for Cordova with

typings install dt~cordova --save --global 

Then after importing your plugin from 'ionic-native' add the following line

declare var cordova:any;

Now you can use the syntax

cordova.somePlugin.someMethod() 

To access your plugin API .

Note: We also publish our tutorials on Medium and DEV.to. If you prefer reading in these platforms, you can follow us there to get our newest articles.

You can reach the author via Twitter:

About the author

Ahmed Bouchefra
is a web developer with 5+ years of experience and technical author with an engineering degree on software development. You can hire him with a click on the link above or contact him via his LinkedIn account. He authored technical content for the industry-leading websites such as SitePoint, Smashing, DigitalOcean, RealPython, freeCodeCamp, JScrambler, Pusher, and Auth0. He also co-authored various books about modern web development that you can find from Amazon or Leanpub


Get our Learn Angular 8 in 15 Easy Steps ebook in pdf, epub and mobi formats, plus a new Angular 8 tutorial every 3 days.


Online Courses (Affiliate)

If you prefer learning with videos. Check out one of the best Angular courses online
Angular 8 - The Complete Guide (2019+ Edition)

Angular Crash Course for Busy Developers

comments powered by Disqus
DMCA.com Protection Status