angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Using invalid environment causes error in build (was "ERROR in Cannot read property 'length' of undefined")

Open elvisbegovic opened this issue 9 years ago • 169 comments

(on existing project, on windows10) After updating :

  1. cli 31 to rc0
  2. angular packages to rc1
  3. 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 ?

elvisbegovic avatar Feb 27 '17 12:02 elvisbegovic

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.

s2zaman avatar Nov 29 '17 07:11 s2zaman

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!

gizm0bill avatar Nov 29 '17 15:11 gizm0bill

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.

NoCopy avatar Nov 30 '17 21:11 NoCopy

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 avatar Dec 12 '17 17:12 cyberbeast

@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.

simonneedham avatar Dec 14 '17 11:12 simonneedham

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.

guify avatar Dec 14 '17 22:12 guify

For me it was missing environment files. Adding them fix the issue

sam-rusty avatar Dec 16 '17 20:12 sam-rusty

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.

kevintanhongann avatar Dec 29 '17 14:12 kevintanhongann

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)

ErikSom avatar Dec 30 '17 14:12 ErikSom

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.

akashzcoder avatar Jan 07 '18 04:01 akashzcoder

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 fresh npm 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"

jcardoz avatar Jan 07 '18 21:01 jcardoz

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.

FunnyGhost avatar Jan 08 '18 14:01 FunnyGhost

I have the exact same problem as @FunnyGhost (angular-cli 1.6.3, angular 4.4.6, typescript 2.6.2)

mittinatten avatar Jan 08 '18 17:01 mittinatten

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!

FunnyGhost avatar Jan 09 '18 10:01 FunnyGhost

Worked for me too. Thanks!

mittinatten avatar Jan 09 '18 11:01 mittinatten

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 ?

bobbajs avatar Jan 10 '18 05:01 bobbajs

Also same "hot-rebuild" issue as @FunnyGhost. Reverting cli from 1.6.3 to 1.6.1 also fixed for me.

VororRich avatar Jan 10 '18 14:01 VororRich

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

winfredchen avatar Jan 15 '18 11:01 winfredchen

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.

qng5150 avatar Jan 18 '18 12:01 qng5150

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.

prabhanjan2906 avatar Jan 22 '18 22:01 prabhanjan2906