Cannot add and import DeviceMotion from ionic-native in ionic 3.4


#1

"cordova plugin add cordova-plugin-device-motion "
I used above command to add device-motion plugin to ionic 3 project.

But I cannot import DeviceMotion to my class using “import {DeviceMotion} from ‘ionic-native’”. It shows me the error cannot find module ionic-native in my IDE (VSCode).

Am I missing typing file (Typescript file) for device-motion plugin or something else.
Can you help me to solve this.
Thank you


#2

Read the documentation please: https://ionicframework.com/docs/native/device-motion/


#3

I read it and try to execute the first command. But it does not work. It gives me the error “cordova is not a valid task


#4

Post your ionic info output and package.json content please.


#5

Thank you! I figured it out.
In ionic documentation it says to run “ionic cordova plugin add cordova-plugin-device-motion” but the actual command should be “ionic plugin add cordova-plugin-device-motion” (without the “cordova” part).

Anyway here are the Ionic Info

Cordova CLI: 7.0.1
Ionic Framework Version: 3.4.2
Ionic CLI Version: 2.2.2
Ionic App Lib Version: 2.2.1
Ionic App Scripts Version: 1.3.7
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 3.19
Node Version: v7.8.0
Xcode version: Not installed


#6

No, the documentation of course refers to the current version of the CLI which uses the command that is documented.


#7

But I was unable to run the command with cordova part. Without that I could run the command and also the plugin can be imported to classes. When I run ionic in terminal. It also suggest to use plugin to add cordova plugins.

 (_)           (_)         
  _  ___  _ __  _  ___     
 | |/ _ \| '_ \| |/ __| 
 | | (_) | | | | | (__     
 |_|\___/|_| |_|_|\___|  CLI v2.2.2

Usage: ionic task args


=======================
Available tasks: 
(use --help or -h for more info)


   start  ..........  Starts a new Ionic project in the specified PATH
   serve  ..........  Start a local development server for app dev/testing
   setup  ..........  Configure the project with a build tool (beta)
   generate  .......  Generate pages and components
   platform  .......  Add platform target for building an Ionic app
   run  ............  Run an Ionic project on a connected device
   emulate  ........  Emulate an Ionic project on a simulator or emulator
   build  ..........  Build (prepare + compile) an Ionic project for a given platform.

   plugin  .........  Add a Cordova plugin

#8

Yes, but you are using an old version of ionic. Run npm install -g ionic to upgrade to the latest version if you want.


#9

Yeah that was the issue. Thank you for the help.