Search code examples
ionic-frameworkcordova-plugins

Cannot find module '@ionic-native/power-management/ngx'


I installed cordova power management plugin into my ionic application. Following the usage guidance "https://ionicframework.com/docs/native/power-management#installationenter link description here", I import the class as below:

import { PowerManagement } from '@ionic-native/power-management/ngx';

but, when build this ionic app, got the error:

Cannot find module '@ionic-native/power-management/ngx'

Why this error appears? How to fix it?

I try to import the class with removing the ngx module, then error disappears:

import { PowerManagement } from '@ionic-native/power-management';

my package.json file is:

{
  "name": "ionicapp",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "start": "ionic-app-scripts serve",
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint"
  },
  "dependencies": {
    "@angular/animations": "5.2.11",
    "@angular/common": "5.2.11",
    "@angular/compiler": "5.2.11",
    "@angular/compiler-cli": "5.2.11",
    "@angular/core": "^5.2.11",
    "@angular/forms": "5.2.11",
    "@angular/http": "5.2.11",
    "@angular/platform-browser": "5.2.11",
    "@angular/platform-browser-dynamic": "5.2.11",
    "@angular/router": "^6.1.9",
    "@ionic-native/background-geolocation": "^4.16.0",
    "@ionic-native/background-mode": "^4.20.0",
    "@ionic-native/ble": "^4.12.0",
    "@ionic-native/bluetooth-serial": "^4.12.2",
    "@ionic-native/camera": "4.3.3",
    "@ionic-native/core": "^4.12.2",
    "@ionic-native/geolocation": "^4.12.0",
    "@ionic-native/google-maps": "^4.12.0",
    "@ionic-native/launch-navigator": "^4.15.0",
    "@ionic-native/native-storage": "^4.12.0",
    "@ionic-native/network": "^4.12.2",
    "@ionic-native/network-interface": "^4.12.2",
    "@ionic-native/power-management": "^4.20.0",
    "@ionic-native/splash-screen": "~4.11.0",
    "@ionic-native/sqlite": "^4.12.2",
    "@ionic-native/status-bar": "^4.11.0",
    "@ionic/storage": "^2.1.3",
    "@ngx-translate/core": "8.0.0",
    "@ngx-translate/http-loader": "^2.0.0",
    "@types/google-maps": "^3.2.0",
    "@types/googlemaps": "^3.30.12",
    "an": "0.0.8",
    "angularfire2": "^5.0.2",
    "chart.js": "^2.7.2",
    "cordova-android": "7.1.0",
    "cordova-plugin-actionsheet": "^2.3.3",
    "cordova-plugin-background-mode": "git+https://github.com/tushe/cordova-plugin-background-mode.git",
    "cordova-plugin-ble-central": "^1.2.2",
    "cordova-plugin-bluetooth-serial": "^0.4.7",
    "cordova-plugin-compat": "^1.2.0",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-dialogs": "^2.0.1",
    "cordova-plugin-geolocation": "^4.0.1",
    "cordova-plugin-googlemaps": "^2.4.2",
    "cordova-plugin-ionic-keyboard": "^2.1.2",
    "cordova-plugin-ionic-webview": "^2.1.0",
    "cordova-plugin-mauron85-background-geolocation": "^3.0.0-alpha.43",
    "cordova-plugin-nativestorage": "^2.3.2",
    "cordova-plugin-network-information": "^2.0.1",
    "cordova-plugin-networkinterface": "^2.0.0",
    "cordova-plugin-powermanagement-orig": "^1.1.2",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "cordova-sqlite-storage": "^2.4.0",
    "firebase": "^5.5.3",
    "ionic-angular": "3.9.2",
    "ionic-native": "4.1.1",
    "ionicons": "3.0.0",
    "ng2-charts": "^1.6.0",
    "rxjs": "5.5.11",
    "sw-toolbox": "3.6.0",
    "uk.co.workingedge.phonegap.plugin.launchnavigator": "^4.2.2",
    "zone.js": "0.8.26",
    "at.gofg.sportscomputer.powermanagement": "~1.1.2"
  },
    "devDependencies": {
    "@ionic/app-scripts": "^3.2.0",
    "@mauron85/cordova-plugin-background-geolocation": "^3.0.0-alpha.4",
    "typescript": "~2.6.2"
  },
  "description": "An Ionic project",
  "cordova": {
     "plugins": {
      "cordova-plugin-ble-central": {},
      "cordova-plugin-whitelist": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-plugin-ionic-keyboard": {},
      "cordova-plugin-geolocation": {
        "GEOLOCATION_USAGE_DESCRIPTION": "To locate you"
      },
      "cordova-plugin-mauron85-background-geolocation": {
        "GOOGLE_PLAY_SERVICES_VERSION": "11+",
        "ANDROID_SUPPORT_LIBRARY_VERSION": "23+",
        "ALWAYS_USAGE_DESCRIPTION": "App requires background tracking enabled",
        "MOTION_USAGE_DESCRIPTION": "App requires motion detection"
      },
      "cordova-plugin-nativestorage": {},
      "cordova-sqlite-storage": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-bluetooth-serial": {},
      "cordova-plugin-network-information": {},
      "cordova-plugin-networkinterface": {},
      "uk.co.workingedge.phonegap.plugin.launchnavigator": {
        "OKHTTP_VERSION": "3.+"
      },
      "cordova-plugin-background-mode": {},
      "at.gofg.sportscomputer.powermanagement": {}
    },
    "platforms": [
      "android"
    ]
  }
}

Solution

  • Use ngx if your Angular version is >=6

    You seem to be using the wrong plugin version. Correct your error by following the steps mentioned below

    Check your project type in ionic.config.json file.

    If the type is "ionic-angular", then install 4 version.

    ionic cordova plugin add cordova-plugin-powermanagement-orig
    npm install @ionic-native/power-management@4
    

    If the type is "angular", then install the latest version

    ionic cordova plugin add cordova-plugin-powermanagement-orig
    npm install @ionic-native/power-management
    

    Note:

    Add ngx at the end of import only if you are using Angular 6 or 6+

    import { PowerManagement } from '@ionic-native/power-management/ngx';
    

    if not remove ngx from the import both in app.module.ts and app.component.ts

    import { PowerManagement } from '@ionic-native/power-management';
    

    Refencence:https://github.com/ionic-team/ionic/issues/15225#issuecomment-414074074