0

I am using ionic2 build . I did ionic plugin add cordova-plugin-file and used following code.

import {File} from 'ionic-native';
@Injectable()
export class GlobalVars {
  constructor(platform:Platform) {
    platform.ready().then(() => {
       this.appRootFolder = cordova.file.documentsDirectory;
    }
  }
}

then I did ionic build android and I got this error

Cannot find name 'cordova'

After 2 hour of struggling , I resolved the issues by following commands

npm install -g typings
typings install dt~cordova --save --global 
typings install dt~cordova/plugins/filesystem --save  --global

This helped in building android , but still fails for iOS. when I run this ionic build ios I still get

Cannot find name 'cordova'

3 Answers 3

8

I wrote this line(declare var cordova:any;) at the top of file

import {File} from 'ionic-native';
declare var cordova:any;

And the problem is solved for me.

2

As of lately, you can do this:

In CLI, from your project folder:

ionic plugin add cordova-plugin-file

Then, in your component/class file:

import { File } from 'ionic-native';

declare var cordova: any;
const fs:string = cordova.file.dataDirectory;
File.checkDir(this.fs, 'mydir')
    .then(_ => console.log('yay'))
    .catch(err => console.log('boooh'));

Many native plugins are now well implemented and documented by the Ionic Team :)

Source: Ionic Native docs

0

Try copying cordova.d.ts file and plugins folder from here - https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/cordova230

And paste this folder and file in your project/typings directory.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.