找不到模块'@ angular / core'。找不到模块'rxjs'

时间:2018-05-15 01:34:50

标签: angular firebase ionic-framework angularfire2

环境:

操作系统:Linux Mint 18 Cinnamon 64位
NPM:5.6.0
节点:v10.0.0
离子:3.20.0

我是如何收到此错误的

我跟随this tutorial使用angularfire2将firebase应用于离子项目。我完全按照教程,直到我在运行

时收到错误
$ ionic serve

完整的错误是这样的:

typescript: home/brian/node_modules/angularfire2/angularfire2.d.ts, line: 1 
Cannot find module '@angular/core'. 

L1:  import { InjectionToken, NgZone } from '@angular/core';
L2:  import { Observable, Subscription } from 'rxjs';

typescript: home/brian/node_modules/angularfire2/angularfire2.d.ts, line: 2 
Cannot find module 'rxjs'. 

L1:  import { InjectionToken, NgZone } from '@angular/core';
L2:  import { Observable, Subscription } from 'rxjs';
L3:  import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';

typescript: home/brian/node_modules/angularfire2/auth/auth.d.ts, line: 3 
Cannot find module '@angular/core'. 

L2:  import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';
L3:  import { NgZone } from '@angular/core';
L4:  import { Observable } from 'rxjs';

typescript: home/brian/node_modules/angularfire2/auth/auth.d.ts, line: 4 
Cannot find module 'rxjs'. 

L3:  import { NgZone } from '@angular/core';
L4:  import { Observable } from 'rxjs';
L5:  export declare class AngularFireAuth {

typescript: home/brian/node_modules/angularfire2/firebase.app.module.d.ts, line: 1 
Cannot find module '@angular/core'. 

L1:  import { InjectionToken } from '@angular/core';
L2:  import { FirebaseApp as _FirebaseApp, FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types';

回溯

追溯,当我通过命令行安装angularfire2时,我运行此命令(following the documentation

npm install firebase angularfire2 --save

然后它返回:

npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning EAI_AGAIN: request to https://registry.npmjs.org/firebase failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org registry.npmjs.org:443
npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation.
npm WARN angularfire2@5.0.0-rc.7 requires a peer of @angular/common@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.7 requires a peer of @angular/core@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.7 requires a peer of @angular/platform-browser@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.7 requires a peer of @angular/platform-browser-dynamic@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.7 requires a peer of @firebase/app@^0.1.6 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.7 requires a peer of rxjs@^5.5.4 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.7 requires a peer of zone.js@^0.8.0 but none is installed. You must install peer dependencies yourself.
npm WARN bin@1.0.0 No description
npm WARN bin@1.0.0 No repository field.

+ angularfire2@5.0.0-rc.7
+ firebase@5.0.2
updated 2 packages in 80.502s

然后我尝试再次运行相同的命令,现在它返回几乎与上面相同,但没有 npm WARN注册表

npm WARN angularfire2@5.0.0-rc.8.0 requires a peer of @angular/common@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.8.0 requires a peer of @angular/core@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.8.0 requires a peer of @angular/platform-browser@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.8.0 requires a peer of @angular/platform-browser-dynamic@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.8.0 requires a peer of rxjs@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angularfire2@5.0.0-rc.8.0 requires a peer of zone.js@^0.8.0 but none is installed. You must install peer dependencies yourself.
npm WARN bin@1.0.0 No description
npm WARN bin@1.0.0 No repository field.

package.json 文件位于/ home / brian中,它包含以下行。

{
  "name": "bin",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "angular": "^1.7.0",
    "angularfire2": "^5.0.0-rc.8.0",
    "cordova": "^8.0.0",
    "firebase": "^5.0.2",
    "ionic": "^3.20.0",
    "npm": "^6.0.1"
  }
}

运行$ ionic serve

后,浏览器中有此文本
Ionic Framework: 3.9.2
Ionic App Scripts: 3.1.9
Angular Core: 5.2.10
Angular Compiler CLI: 5.2.10
Node: 10.0.0
OS Platform: Linux 4.4
Navigator Platform: Linux x86_64
User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
  

我想我在与npm和node相同的位置安装了angularfire,   因为我在默认位置运行所有这些安装命令   打开命令行时。它应该在/ home / brian中。我也有   这个文件夹/ home / brian / node_modules。

我的Google研究解决了这个问题

This similar thread说我们需要将@angular重命名为@ angular2,但我发现它返回相同的错误。

有没有人遇到同样的问题?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

根据您ionic v 3.9.2 的版本信息,您需要安装angular V4.4.6,请参阅离子 dependencies 。顺便说一句,如果您没有声明要安装的确切版本,npm将默认安装最新版本

您可以使用以下命令:

// all angular packages listed in above dependencies 
npm install @angular/[package-name]@4.4.6

根据角度套餐的版本,您应安装 **v5.0.0-rc.3AngularFire2 v5.0.1+(not v6.x) **和rxjs

npm install firebase@4.5.0 angularfire2@5.0.0-rc.3 --save
npm install rxjs@5.0.1 --save

答案 1 :(得分:0)

感谢@Pengyy,您的建议非常接近修复它,只需再添加一步。

此错误是由我使用的非兼容版本引起的 - @ angular / core - firebase - angularfire2 - 和rxjs。

我解决它的方法是关注@Penggyy的回答:

在我的主文件夹/home/brian中运行

  1. npm install @angular/[package-name]@4.4.6
  2. npm install firebase@4.5.0 angularfire2@5.0.0-rc.3 --save
  3. npm install rxjs@5.0.1 --save
  4. 然后需要再做一步:在我运行离子服务的离子项目文件夹中,(例如项目名称为myApp,文件夹为/home/brian/myApp)运行此命令。

    1. npm install firebase@4.5.0
    2. 最后一步是必要的,否则我仍然会收到错误

      Runtime Error 
      Cannot find module "firebase/auth"
      

      这个问题解决了!