Angular CLI - Typescript throws: Cannot read prope

2020-06-01 07:25发布

问题:

I run in angular 4 project with ng serve and i get error

Cannot read property 'length' of undefined

but I don't have any property length in my project..

the full error

    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"
  }
}

回答1:

You might be missing one or more files that typescript is trying to process. To find out which file you are missing just add

console.log(fileName);

at the starting of the createSourceFile function at node_modules/typescript/lib/typescript.js file.



回答2:

Most likely you have changed around your modules in a way that needs the CLI to be restarted.

I had the same error running ng serve after removing a component from a module. By restarting that process and re running ng serve the error went away.



回答3:

My problem was that, for some reason, my envioroment.ts file was missing. So check important files.



回答4:

My error was caused by c&p error when creating a small component. I forgot to change path to component's template and styles.



回答5:

In my case I did the following and it solved the issue:

  1. Create environments folder (if not exist) in src of the Project C:\Users\abcd\Desktop\MyApp\src

  2. Put the two files environment.ts and environment.prod.ts in environments folder.



回答6:

I solved this problem (error during refactoring test). All is good for angular v4.3.* and v4.4*

  1. Change @angular/cli version 1.7.3 to 1.4.10 in package.json (latest for angular4)
  2. rm -rf node_modules
  3. npm install --no-optional


回答7:

Close the running port and Try rebuilding the app with "ng build"



回答8:

In my case, it's mysterious. But it won't hurt to offer what I had witnessed and maybe a bit history. None or maybe some historical event might have caused it but I really don't know.

  • Everything is good. Angular 8 running inside 18.04.4 LTS (Bionic Beaver)LXC within 19.10 ubuntu host with npm 6.14.2, node10.16.3
  • Upgraded host to 20.04 LTS(Focal Fossa). Write some code. Remove a package called angular-shepherd. Try to build --prod. This errors shows up during angular Generating ES5 bundles.
  • Tried everything. All failed.
  • Reset what I did inside the project. Still fail.
  • Reboot LXC.
  • Fixed.