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 ?
i suspect this length issue may be caused by File path too long on windows, keep below 240 characters, which is mostly encountered on Windows OS. so if this stackoverflow-39621250 may help in it.
in @ngtools/webpack it says you can configure basePath but in the process it ends up here
https://github.com/angular/angular/blob/c2b3792a3b5fa5215fe2ef7e0ac6511086ffe4c1/packages/compiler-cli/src/perform_compile.ts#L116
and it is all messed up to be honest!
This appears to be a discrepancy issue with custom environment files / paths. running : "hmr": "ng serve --hmr --env=hmr" produces ERROR in TypeError: Cannot read property 'length' of undefined
Logging filename & sourceText in function createSourceFile outputs: filename: environment.ts (<--- should this not be environment.hmr.ts ?) sourceText: undefined
WARNING: to anyone using @simonv3 solution above (adding src/...). Although it does work, it will also BREAK the environment on build (always using the development env)
running "start": "ng serve" seems to work without issue however.
So what is the final solution to this error? Can someone post step by step measures for those who are facing this issue but would probably not want to read through the entire thread?
@cyberbeast In summary, the issue is caused by a misconfigured entry in a project's .angular-cli.json file. It's usually an incorrect file path, so either a file has disappeared/moved/wasn't-checked-in-and-so-isn't-present-on-a-different-dev-machine or the .angualr-cli.json was just edited incorrectly.
If you hit this issue you just have to work through each file referenced and make sure it exists. This can be a little tricky as the paths are relative. You could consider removing non-essential lines to get a working build and then add paths back one by one till you find the path that doesn't work.
I think the proper resolution should be that the CLI should tell you what file it couldn't find, and the absolute path where it expected the file to be, instead of throwing a cannot read property 'length' of undefined message at you.
The root cause is a poor error message in the TypeScript system. Therefore the solution would be a patch to TypeScript that improves the error message to tell the user WHICH file is missing from .angular-cli.json. Also angular.io should say something about this! They paint such a rosy picture when selling Angular.
For me it was missing environment files. Adding them fix the issue
I think I'm panicking.
ERROR TypeError: Cannot read property 'length' of undefined
at eval (http.js:123)
at Array.forEach (<anonymous>)
at HttpHeaders.lazyInit (http.js:117)
at HttpHeaders.init (http.js:265)
at HttpHeaders.forEach (http.js:368)
at Observable.eval [as _subscribe] (http.js:2172)
at Observable._trySubscribe (Observable.js:172)
at Observable.subscribe (Observable.js:160)
at subscribeToResult (subscribeToResult.js:23)
at MergeMapSubscriber._innerSub (mergeMap.js:138)
And it's not because I was missing the environment files declaration.
I had the same issue, for me it was caused by the following code:
if(!(Object.prototype.defineProperty instanceof Function)
&& Object.prototype.__defineGetter__ instanceof Function
&& Object.prototype.__defineSetter__ instanceof Function)
{
Object.defineProperty = function(obj, p, cfg) {
if(cfg.get instanceof Function)
obj.__defineGetter__(p, cfg.get);
if(cfg.set instanceof Function)
obj.__defineSetter__(p, cfg.set);
}
}
Commenting the above out fixed it for me.
Still a library i'm using (Box2D) has this implemented, anyone any idea on how to fix this?
EDIT
Fixed it myself :) the code above had a bug in it, if anyone happens to use box2dweb, make sure that
if(!(Object.prototype.defineProperty instanceof Function)
is
if(!(Object.defineProperty instanceof Function)
I had a similar issue when I made some mistake in the main.ts file. So, I pulled an older version and it worked fine for me.
So ran into this issue and was stuck on it for two days.
Checked all environment.ts files and additional file paths in angular-cli.json
Angular refused to rebuild after I made any change.
I tried the following before resorting to the fix below:
-
Clearing out the node_modules using
rm -rf node_modules/*and then doing a freshnpm install -
Re-cloning the entire repo
Finally the only thing that resolved my issue was downgrading the @ngtools/webpack plugin from version 1.9.0 to 1.8.2
I am not sure if this is the right fix, but it worked for me. I'm adding this comment here because it might save someone some time down the line.
My Configuration:
OS: Mac OSX El Capitan 10.11.6
"@angular/cli": "1.4.2"
"typescript": "~2.3.3"
"webpack": "~3.5.5"
I have the same issue in one of my projects. What is different is that this happens only on ng-test hot reload. So, the first run it works, then, if I save any file it fails.
ng-serve works with hot-reload just fine.
So, I added console.log in the createSourceFile method and this is what it says: Filename to create: /Users/utukku/Documents/Projects/frontend-app/src
So, it's not even a file. It's a folder. Not sure what to make of this. Any help is highly appreciated.
I have the exact same problem as @FunnyGhost (angular-cli 1.6.3, angular 4.4.6, typescript 2.6.2)
So, managed to trace back the issue. In my case, downgrading @angular/cli from 1.6.3 to 1.6.1 made it work. Not sure what exactly the issue is but I just ended up looking for differences in package-lock.json. Hope it helps!
Worked for me too. Thanks!
I had same issue as @FunnyGhost . I followed the combination of @jcardoz and @FunnyGhost and it works like a charm. Thanks!
However - does this mean that this is a bug in @angular/cli version 1.6.3 ?
Also same "hot-rebuild" issue as @FunnyGhost. Reverting cli from 1.6.3 to 1.6.1 also fixed for me.
Had this issue when run: ng build --prod
Fixed by:
npm uninstall -g @angular/cli
npm uninstall --save-dev @angular/cli
npm install -g @angular/[email protected]
rmdir /s node_modules
npm install --save-dev @angular/[email protected]
npm install
also, add environment.prod.ts under \src\environments
export const environment = {
production: true
};
I have the exact same problem as @FunnyGhost (angular-cli 1.6.3, angular 4.4.6, typescript 2.3.4). Downgrading the angular-cli to 1.6.1 (globally and locally) resolved my problem.
I have the same problem when running karma unit tests. here is my findings
one of my unit test will write data to a json file. and i get this error
ERROR in TypeError: Cannot read property 'length' of undefined
at createSourceFile (...\node_modules\typescript\lib\typescript.js:15460:109)
at parseSourceFileWorker (...\node_modules\typescript\lib\typescript.js:15389:26)
at Object.parseSourceFile (...\node_modules\typescript\lib\typescript.js:15338:26)
at Object.createSourceFile (...\node_modules\typescript\lib\typescript.js:15192:29)
at new TypeScriptFileRefactor (...\node_modules\@ngtools\webpack\src\refactor.js:79:35)
at Object.findLazyRoutes (...\node_modules\@ngtools\webpack\src\lazy_routes.js:18:22)
at AotPlugin._findLazyRoutesInAst (...\node_modules\@ngtools\webpack\src\plugin.js:220:50)
at _donePromise.Promise.resolve.then.then.then.then.then (...\node_modules\@ngtools\webpack\src\plugin.js:499:24)
at process._tickCallback (internal/process/next_tick.js:109:7)
I added a console.log statements at createSourceFile() and at AotPlugin._findLazyRoutesInAst.
i noticed the following in AotPlugin._findLazyRoutesInAst.
for (const filePath of changedFilePaths) { }. I added a console.log statement to see what was in the changedFilePaths, I noticed the following:
These are changedFilePaths ==>
[ '.../src/polyfills.ts',
'.../src' <--- Please note the missing file name.
]
Hence, i am getting the above error.
This happens when i make changes to any files (irrespective of .ts files). I tried modifying a json file and the above happened.
I tried the following:
in karma.conf.js (as per the official config docs, i added
exclude: ["./src/nonCodeRelatedData/**"]. and
files: [ {pattern: "./src/nonCodeRelatedData/**", watched: false, included: false, served: false}]
But no luck.
Could someone point me the remedy to this problem? Any help is greatly appreciated.