Ionic应用报错找不到cordova-plugin-appsee,新手求插件导入指导
Hey there! Let's work through this together—since you already have Node.js experience and got past the deviceready hurdle, you're well on your way. The error you're hitting is super common for folks new to Ionic/Cordova, and it boils down to a key difference between regular npm packages and Cordova plugins.
First: Understand Why the Error Happens
Cordova plugins like appsee or uxcam aren't standard npm modules you can import directly. They're native code wrappers that get injected into your app's runtime after the deviceready event fires, and they live on the global window object—not in your node_modules folder. When you try import 'cordova-plugin-appsee', Node's module loader looks for that package in node_modules and can't find it, hence the error.
Step 1: Double-Check the Plugin is Installed
First, make sure you've actually added the plugin to your Cordova project. Run one of these commands in your project root:
- For Cordova directly:
cordova plugin add cordova-plugin-appsee - For Ionic (recommended):
ionic cordova plugin add cordova-plugin-appsee
Verify it's installed with:
cordova plugin list
You should see cordova-plugin-appsee listed here. If not, reinstall it—sometimes network issues can cause partial installs.
Step 2: Use the Plugin Correctly (Two Options)
There are two main ways to use Cordova plugins in Ionic 2: directly via the global object, or using Ionic Native wrappers (if available).
Option A: Use the Global Object (No Ionic Native)
Since Cordova plugins attach to the global window after deviceready, you just need to:
- Tell TypeScript about the global variable (to avoid type errors). At the top of any file where you use Appsee, add:
declare var Appsee: any; - Wait for platform ready (which wraps deviceready) before calling the plugin. In your component (like
app.component.ts):import { Platform } from 'ionic-angular'; @Component({ selector: 'app-root', templateUrl: 'app.html' }) export class MyApp { constructor(private platform: Platform) { this.initializeApp(); } initializeApp() { this.platform.ready().then(() => { // Now deviceready has fired—safe to use Appsee if (typeof Appsee !== 'undefined') { Appsee.start("YOUR_APPSEE_API_KEY"); } }); } }
Option B: Use Ionic Native Wrappers (Cleaner for Angular)
Many popular Cordova plugins have Ionic Native wrappers that make them fit better with Angular's dependency injection. For Appsee:
- Install the Ionic Native package alongside the Cordova plugin:
npm install @ionic-native/appsee - Register the provider in
app.module.ts:import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { IonicModule } from 'ionic-angular'; import { Appsee } from '@ionic-native/appsee'; import { MyApp } from './app.component'; @NgModule({ declarations: [MyApp], imports: [BrowserModule, IonicModule.forRoot(MyApp)], bootstrap: [IonicApp], providers: [ Appsee, // Add Appsee to providers { provide: ErrorHandler, useClass: IonicErrorHandler } ] }) export class AppModule {} - Inject and use it in your component (still need to wait for platform ready):
import { Platform } from 'ionic-angular'; import { Appsee } from '@ionic-native/appsee'; @Component({...}) export class MyApp { constructor(private platform: Platform, private appsee: Appsee) { this.platform.ready().then(() => { this.appsee.start("YOUR_APPSEE_API_KEY"); }); } }
Same Steps Apply to UXCam
For uxcam, the process is identical:
- Install the plugin:
ionic cordova plugin add cordova-plugin-uxcam - Either use the global
UXCamvariable (adddeclare var UXCam: any;first) or find/use its Ionic Native wrapper if available. - Always call its methods after
platform.ready().
Critical Note: Testing
Cordova plugins won't work in the browser when you run ionic serve—they require a native device or simulator. Test your app with:
ionic cordova run android # OR ionic cordova run ios
内容的提问来源于stack exchange,提问作者emma_lynn_rose

