Using invalid environment causes error in build (was "ERROR in Cannot read property 'length' of undefined")
(on existing project, on windows10) After updating :
- cli 31 to rc0
- angular packages to rc1
- typescript to 2.1.6
I get this insignifiant message error when running ng serve or ng build:
Cannot read property 'length' of undefined TypeError: Cannot read property 'length' of undefined
What does this mean ?
We'd really like to look into this, can someone please provide a reproduction we can follow to see the problem?
I encountered this error when I started using custom environments. The problem was in my .angular-cli.json, it was a simple typo (made by me!):
broken:
"environments": {
"dev": "environments/environment.ts",
"local_dev": "environments/environment.local_dev.ts",
"aws_qa":"environments.environment.aws_qa.ts",
"prod": "environments/environment.prod.ts"
}
fixed:
"environments": {
"dev": "environments/environment.ts",
"local_dev": "environments/environment.local_dev.ts",
"aws_qa":"environments/environment.aws_qa.ts",
"prod": "environments/environment.prod.ts"
}
The error message was pretty unhelpful, luckily I noticed the typo!
ERROR in TypeError: Cannot read property 'length' of undefined at createSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:16640:110) at parseSourceFileWorker (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:16572:26) at Object.parseSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:16521:26) at Object.createSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:16371:29) at VirtualFileStats.getSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules@ngtools\webpack\src\compiler_host.js:67:35) at WebpackCompilerHost.getSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules@ngtools\webpack\src\compiler_host.js:207:38) at findSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70246:29) at processImportedModules (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70393:25) at findSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70274:17) at processImportedModules (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70393:25) at findSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70274:17) at processImportedModules (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70393:25) at findSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70274:17) at processImportedModules (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70393:25) at findSourceFile (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70274:17) at args (D:\Richard\Documents\workspace\registerinterest\node_modules\typescript\lib\typescript.js:70200:85)
Reverting to 1.6.1 fixed it temporarily for me. I couldn't figure out what file was giving the trouble.
Downgrading @angular/cli to version 1.4.10 solved the issue for me
Happening to me, but I'm not using cli, I'm using ngTools webpack and compiler-cli.
Same here, running unit tests in watch mode results in this issue on re-build. Downgrading to Angular CLI 1.6.1 seems to fix it (temporarily).
Same with me-- error on unit test re-build. Downgrading works.
Same here when using ng-cli 1.6.7 Downgrading to 1.6.1 avoids the problem.
I followed the instruction to put a console.log() call in typescript.js. Funnily, my angular project fails when it tries to read:
0% compiling
Trying to read file: /home/js/myproject/src
Obviously, this isn't a source file - it's a directory where my sources are kept, but for some reason ng cli tries to read it like it was a file and fails miserably. Any ideas what might be wrong in this case?
Can confirm that downgrading to 1.6.1 worked from 1.6.7. But one of my developers has it working fine with 1.6.5 which did not work for me. So unsure of what is actually going on.
Also had a environments error in my angular.json (thanks to @RichyRoo5by5 and others for pointing me in the right direction). I had renamed my environments file but didn't update my angular-cli.json file.
In my case i discovered that my environments.dev.ts file was missing, all of a sudden. Restored it from git and now this problem disappeared.
I have the same issue (more accurately described in 9558 since it has to do with re-loads, the test runner works fine on the first ng test). I downgraded the CLI temporarily to 1.6.1 but in doing so, I had to install @angular-devkit/core as a dev dependency (yarn add --dev @angular-devkit/core) as well to get it to compile.
This may also happen, when you have defined an environment-file in .angular-cli.json but the environment.ts file it self doesn't exist.
I think I have narrowed down the cause of this error. My environment files etc all appear to match those of a new ng5 app so that wasn't my issue. This line in the @ngtools/webpack plugin is why my tests fail on the second run:
https://github.com/angular/angular-cli/blob/master/packages/%40ngtools/webpack/src/plugin.ts#L622-L624
this._discoveredLazyRoutes = this.firstRun
? this._getLazyRoutesFromNgtools()
: this._findLazyRoutesInAst();
It looks like there is a bug in _findLazyRoutesInAst() that only affects ng4 apps.
this._getLazyRoutesFromNgtools() Works fine and is used on the first pass.
this._findLazyRoutesInAst(); Is used on the second pass and always results in an eventual length of undefined error.
I am going to debug a little more and see what each function returns. For now, locally, replacing that ternary statement with this._discoveredLazyRoutes = this._getLazyRoutesFromNgtools(); allows me to run my tests normally.
I am actually able to run ng serve and ng test normally, the watcher works well for ng serve, but I got this error when I update any file while I am on ng test, after looking at the comments, I made sure I have environment.ts and no issues on my tests.ts (even rolling back to previous versions). Doing what @mhamel06 worked well, but I was wondering if there is something else that we can check on our end just to fix the watcher loop on ng test mode, any thoughts on karma.conf and webpack conf maybe just to avoid this?.
Thanks @FunnyGhost, I just downgraded cli version and it worked to me.
+1 for me
Same as @mhamel06 - seems to be something with the _findLazyRoutesInAst() call...
+1 we have the same problem
I was with the same problem. After 'googling", my issue was resolved by running this:
- npm install typescript@'>=2.1.0 <2.4.0'
(after run "ng serve", console displayed "run the following command to install a compatible version of TypeScript.";
- After step 1, console displayed:
npm WARN [email protected] requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself. npm WARN [email protected] requires a peer of firebase@^3.6.6 but none is installed. You must install peer dependencies yourself.
So, i run:
-
npm install ajv@^6.0.0
-
npm install firebase@^3.6.6
After that, all is right!
I hope i have helped!