cordova-plugin-file in Chrome: cordova is not defined

Sato picture Sato · Jul 12, 2015 · Viewed 30.5k times · Source

I use cordova-plugin-file in my ionic app to download images and save to local.

When I run it in emulator or iphone, there is no error, but when I test it in Chrome, it says, cordova is not defined when I try to access cordova.file.dataDirectory

How can I run cordova-plugin-file in chrome?

Answer

LeftyX picture LeftyX · Jul 12, 2015

Cordova will not be available in the browser; only on your device.

In your index.html you should have this script reference:

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

As the note says, the script won't be available during the development. It will be replaced during the build process.

You can try it yourself:

cordova platform add android

then

cordova build

and you should find under platforms\android\assets\www 2 js files: cordova.js and cordova_plugins.js.

Another option is to add browser as platform:

cordova platform add browser

and the run it:

cordova run browser

but you might face a few troubles.