Angular CLI - Typescript throws:无法读取未定义的属性“length”

时间:2017-07-20 15:56:43

标签: angular angular-cli

我使用ng服务在角度4项目中运行,我得到错误

Cannot read property 'length' of undefined

但我的项目中没有任何属性长度..

完整错误

    Your global Angular CLI version (1.2.1) is greater than your local
version (1.1.3). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at createSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:15457:109)
    at parseSourceFileWorker (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:15389:26)
    at Object.parseSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:15338:26)
    at Object.createSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:15192:29)
    at VirtualFileStats.getSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\@ngtools\webpack\src\compiler_host.js:66:35)
    at WebpackCompilerHost.getSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\@ngtools\webpack\src\compiler_host.js:213:38)
    at findSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:67909:29)
    at processImportedModules (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:68056:25)
    at findSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:67937:17)
    at processImportedModules (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:68056:25)
    at findSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:67937:17)
    at processSourceFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:67840:27)
    at processRootFile (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:67728:13)
    at E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:67018:60
    at Object.forEach (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:1449:30)
    at Object.createProgram (E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master\node_modules\typescript\lib\typescript.js:67018:16)
PS E:\פרוייקטים\פרוייקטים קיץ\Angular 4\Youtube Channel Angular Firebase\full project of the instructor github\angular-firestarter-master>

的package.json

{
  "name": "firestarter",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.2.4",
    "@angular/common": "^4.2.4",
    "@angular/compiler": "^4.2.4",
    "@angular/core": "^4.2.4",
    "@angular/forms": "^4.2.4",
    "@angular/http": "^4.2.4",
    "@angular/platform-browser": "^4.2.4",
    "@angular/platform-browser-dynamic": "^4.2.4",
    "@angular/platform-server": "^4.2.4",
    "@angular/router": "^4.2.4",
    "@types/lodash": "^4.14.67",
    "angularfire2": "^4.0.0-rc.1",
    "core-js": "^2.4.1",
    "firebase": "^4.1.3",
    "lodash": "^4.17.4",
    "rxjs": "^5.4.1",
    "zone.js": "^0.8.12"
  },
  "devDependencies": {
    "@angular/cli": "^1.1.3",
    "@angular/compiler-cli": "^4.2.4",
    "@types/jasmine": "2.5.53",
    "@types/node": "~8.0.4",
    "codelyzer": "~3.1.1",
    "jasmine-core": "~2.6.4",
    "jasmine-spec-reporter": "~4.1.1",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.3.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.1.0",
    "tslint": "~5.4.3",
    "typescript": "=2.3.4"
  }
}

9 个答案:

答案 0 :(得分:21)

您可能缺少打字稿正在尝试处理的一个或多个文件。要找出您缺少的文件,请添加

  

的console.log(文件名);

在node_modules / typescript / lib / typescript.js文件的createSourceFile函数的开头。

答案 1 :(得分:6)

您很可能已经以需要重新启动CLI的方式更改了模块。

从模块中删除组件后,我遇到了运行ng serve的错误。通过重新启动该流程并重新运行ng serve,错误就消失了。

答案 2 :(得分:5)

我的问题是,由于某种原因,我的envioroment.ts文件丢失了。所以检查重要文件。

答案 3 :(得分:1)

我的错误是由创建小组件时的c&p错误引起的。我忘了更改组件模板和样式的路径。

答案 4 :(得分:1)

可能是组件 templateUrl 的错误路径,可能缺少 ./

之前:templateUrl: 'my-control.component.html',

之后:templateUrl: './my-control.component.html',

答案 5 :(得分:0)

我解决了这个问题(重构测试期间出错)。对于角度v4.3。*和v4.4 *

,一切都很好
  1. package.json 中更新@angular/cli版本1.7.3至 1.4.10 (最新版本为angular4)
  2. rm -rf node_modules
  3. npm install --no-optional

答案 6 :(得分:0)

就我而言,我执行了以下操作,并解决了该问题:

    在项目C:\Users\abcd\Desktop\MyApp\src

    src
  1. 创建环境文件夹(如果不存在)

  2. 将两个文件 environment.ts environment.prod.ts 放入 environments 文件夹中。

答案 7 :(得分:0)

关闭正在运行的端口,然后尝试使用“ ng build”重建应用程序

答案 8 :(得分:0)

就我而言,这很神秘。但是提供我亲眼所见,也许还有一点历史,都不会有什么坏处。没有或者也许是历史事件可能导致了它,但我真的不知道。

  • 一切都很好。 Angular 8在18.04.4 LTS (Bionic Beaver)LXC内的19.10 ubuntu host内部运行,npm 6.14.2,节点10.16.3
  • 将主机升级到20.04 LTS(Focal Fossa)。编写一些代码。删除名为angular-shepherd的软件包。尝试构建--prod。在有角度的生成ES5捆绑包期间会显示此错误。
  • 尝试了一切。全部失败。
  • 重置我在项目中所做的工作。仍然失败。
  • 重新启动LXC。
  • 已修复。