typescript错误找不到模块'firebase / app'

时间:2017-12-21 08:27:02

标签: angular angularfire

我正在尝试将ngx-admin与firebase集成。我已经在我的角度项目中安装了firebase。但是它给了我这样的错误“typescript错误无法找到模块'firebase / app'。”我也尝试安装打字,但没有运气

我的stackstrace

D:\admin-ionic> ng serve
Your global Angular CLI version (1.6.1) is greater than your local
version (1.4.9). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2017-12-21T08:36:34.546Z
Hash: ecdd99ad138ea0774dff
Time: 8026ms
chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered]
chunk {main} main.bundle.js, main.bundle.js.map (main) 1.06 kB {vendor} [initial] [rendered]
chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 323 bytes {inline} [initial] [rendered]
chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 694 kB {inline} [initial] [rendered]
chunk {vendor} vendor.bundle.js, vendor.bundle.js.map (vendor) 338 kB [initial] [rendered]

ERROR in D:/admin-society/node_modules/angularfire2/firestore/observable/fromRef.d.ts (1,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/firebase.app.module.d.ts (3,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/firestore/collection/collection.d.ts (1,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/firestore/document/document.d.ts (1,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/firestore/firestore.d.ts (1,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/firestore/collection/changes.d.ts (1,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/firestore/interfaces.d.ts (2,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/auth/auth.d.ts (1,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/database/interfaces.d.ts (1,27): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/database/database.d.ts (1,26): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/database/list/state-changes.d.ts (3,26): Cannot find module 'firebase/app'.
ERROR in D:/admin-society/node_modules/angularfire2/database/list/audit-trail.d.ts (3,26): Cannot find module 'firebase/app'.
ERROR in Error: Metadata version mismatch for module D:/admin-society/node_modules/angularfire2/index.d.ts, found version 4, expected 3, resolving symbol AppModule in D:/admin-society/src/app/app.module.ts, resolving symbol AppModule in D:/admin-society/src/app/app.module.ts, resolving symbol AppModule in D:/admin-society/src/app/app.module.ts
    at syntaxError (D:\admin-society\node_modules\@angular\compiler\bundles\compiler.umd.js:1729:34)
    at simplifyInContext (D:\admin-society\node_modules\@angular\compiler\bundles\compiler.umd.js:24979:23)
    at StaticReflector.simplify (D:\admin-society\node_modules\@angular\compiler\bundles\compiler.umd.js:24991:13)
    at StaticReflector.annotations (D:\admin-society\node_modules\@angular\compiler\bundles\compiler.umd.js:24418:41)
    at _getNgModuleMetadata (D:\admin-society\node_modules\@angular\compiler-cli\src\ngtools_impl.js:138:31)
    at _extractLazyRoutesFromStaticModule (D:\admin-society\node_modules\@angular\compiler-cli\src\ngtools_impl.js:109:26)
    at Object.listLazyRoutesOfModule (D:\admin-society\node_modules\@angular\compiler-cli\src\ngtools_impl.js:53:22)
    at Function.NgTools_InternalApi_NG_2.listLazyRoutes (D:\admin-society\node_modules\@angular\compiler-cli\src\ngtools_api.js:91:39)
    at AotPlugin._getLazyRoutesFromNgtools (D:\admin-society\node_modules\@ngtools\webpack\src\plugin.js:212:44)
    at _donePromise.Promise.resolve.then.then.then.then.then (D:\admin-society\node_modules\@ngtools\webpack\src\plugin.js:448:24)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

webpack: Failed to compile.

这是我的代码

environment.ts

export const environment = {
  production: false,
  firebase : {
    apiKey: '***************************************',
    authDomain: 'ionicadmin-123456.firebaseapp.com',
    databaseURL: 'https://ionicadmin-123456.firebaseio.com',
    projectId: 'ionicadmin-123456',
    storageBucket: 'ionicadmin-123456.appspot.com',
    messagingSenderId: '123456789123',
  },
};

app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    HttpModule,
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule, // imports firebase/firestore, only needed for database features
    AngularFireAuthModule, // imports firebase/auth, only needed for auth features
    NgbModule.forRoot(),
    ThemeModule.forRoot(),
    CoreModule.forRoot(),
  ],
  bootstrap: [AppComponent],
  providers: [
    { provide: APP_BASE_HREF, useValue: '/' },
  ],
})

committee.component.ts

import { Component } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'ngx-chartjs',
  styleUrls: ['./committee.component.scss'],
  templateUrl: './committee.component.html',
})
export class CommitteeComponent {
  items: Observable<any[]>;
  constructor(db: AngularFirestore) {
    this.items = db.collection('items').valueChanges();
  }
}

committee.component.html

<ul>
    <li *ngFor="let item of items | async">
      {{ item.name }}
    </li>
  </ul>

4 个答案:

答案 0 :(得分:7)

似乎firebase 4.8.1改变了其打字稿类型。

现在,您可以通过运行以下命令恢复到firebase 4.8.0:

npm uninstall firebase
npm install firebase@4.8.0
rm -rf node_modules
npm install

答案 1 :(得分:2)

我遇到了同样的问题,但是我在package.json可用的目录中运行npm install firebase后解决了该问题。

答案 2 :(得分:1)

这样的环境代码

export const environment = {
  production: false,
  firebase : {
    apiKey: '***************************************',
    authDomain: 'ionicadmin-123456.firebaseapp.com',
    databaseURL: 'https://ionicadmin-123456.firebaseio.com',
    projectId: 'ionicadmin-123456',
    storageBucket: 'ionicadmin-123456.appspot.com',
    messagingSenderId: '123456789123',
  }
};

答案 3 :(得分:1)

在Angular4 +之后,它的工作方式有所不同。以下是将Firebase用于角度项目的步骤:

$ npm install --save firebase @angular/fire -f

接下来,您可以创建和导出用于配置Firebase的常量,或者在Angular项目中打开environment / environment.ts文件,然后在环境对象内添加firebaseConfig对象。 我更喜欢第二种解决方案,因为它可以根据环境而变化

  

export const environment = {生产:false,firebaseAppConfig:   {...}

将所有需要的模块导入到您的自定义模块中,如下所示:

import { AngularFireModule } from '@angular/fire';
import { AngularFireDatabaseModule } from '@angular/fire/database';
import { environment } from '../environments/environment';

@NgModule({
        // [...]
    imports: [
        // [...]
        AngularFireModule.initializeApp(environment.firebaseConfig),
        AngularFireDatabaseModule
    ],

以其他方式,如果您想使用Firebase创建更多角度的项目,请遵循此链接中的说明: Angular Firestore Tutorial