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've received this error when a generated (and referenced) d.ts file was missing. If you receive the error the issue is probably in you project..

/// <reference path="../../someFileThatDidntExist.d.ts" />

CanKattwinkel avatar Apr 27 '17 08:04 CanKattwinkel

thank you for clarifications CanKattwinkel, The question is: Why earlier versions of @angular/cli does not happen ?

Why should i do, ignore in the exclude tag of tsconfig, some important dependency modules?

Some suggestion?

fopnet avatar Apr 27 '17 17:04 fopnet

FWIW in my case this error tripped after a large refactor / rename exercise. It went away after I restarted ng serve.

mflorence99 avatar Apr 29 '17 11:04 mflorence99

I stopped ng serve and restarted it, now the error message is gone.

tomcatmwi avatar May 03 '17 03:05 tomcatmwi

I also had issues with my build. The issue was with the environment files in my angular-cli.json as pointed by @moosetraveller and some others.

Thank you all for big help!

leocaseiro avatar May 05 '17 01:05 leocaseiro

I had the same error. My problem was that the "test.ts" under the "src" folder was missing for some reason. Restored the file from a backup and it is working fine now. The karma.conf.js has a reference to that file as... { pattern: './src/test.ts', watched: false }

Thanks for everyone for pointing me in the right direction.

ndvsss avatar May 10 '17 17:05 ndvsss

I noticed that in a project created (that does not get this error) with ng new (@angular/cli 1.0.6) that the angular-cli.json file was renamed to .angular-cli.json (hidden file).

I renamed the file in my upgrading project but did not help. I mention this because there must be something else that has changed that may be causing the issue.

Although, this does seem like a code incompatibility issue - probably some dependency that is out of sync. But it is not obvious. I brought versions in package.json to the same levels.

klmcwhirter avatar May 28 '17 23:05 klmcwhirter

For what it's worth, I got this error when referencing a d.ts file via: ///<reference path="./foo.d.ts" /> with an invalid path, which I discovered using @klferreira's suggestion, on #5132:

Put a console.log(fileName) in your createSourceFile() function and it will show you exactly where is the problem. Mine was environment.ts that was missing.

jdthorpe avatar Jun 18 '17 20:06 jdthorpe

This error seem to have different reasons and different solutions. In my case I had a reference /// pointing to a file that did not exist.

This happened to me, somehow my 'typings' folder was missing, I just restored it and everything worked fine.

leandrolanzieri avatar Jun 21 '17 12:06 leandrolanzieri

@leandrolanzieri Thanks for the suggestion. This was the problem for me. Was banging my head against the wall for a while.

michaeldaw avatar Jun 28 '17 14:06 michaeldaw

I hit this error when I started trying to work on a clean machine and cloned our repo.

Currently we have an environment.prod.ts & an environment.ts file referenced in our .angular-cli.json, however, it's up to the dev to create a local environment.ts and it's configured to be gitignored.

I forgot to create a local environment.ts and banged my head against this error for 45mins until I remembered to create one.

Like most other people on this thread I would have appreciated a pretty error message telling me that (in my case) "An environment.ts file was specified in .angular-cli.json but could not be found at this 'specific-path'"

simonneedham avatar Jul 18 '17 11:07 simonneedham

I had the same issue. In my case, it was a typo in the environment settings in angular-cli,json. Make sure it spelled correctly like this environments/environment.xyz.ts.

hnrchrdl avatar Jul 21 '17 11:07 hnrchrdl

I had the same issue. In my case, I had to remove some sub-folders under the 'typings' folder, which I forgot to also remove from the index.d.ts file. Hope this helps someone.

kkotak avatar Jul 26 '17 23:07 kkotak

Guys any update on this issue ?

gtzinos avatar Jul 27 '17 12:07 gtzinos

I have some problem in a fwe moments ago... I shell ng serve -e local And i check my .angular-cli.json file and not found "local": "environments/environment.local.ts" Before i check this, problem is solved Try this x) P.S. Sorry for my bad english

WiRight avatar Jul 28 '17 06:07 WiRight

I just had the same issue, and got to resolve it by changing the path to the enviroments folder in my .angular-cli.json. From this:

"environmentSource": "environments/environment.ts",
"environments": {
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
}

To this:

"environmentSource": "src/environments/environment.ts",
"environments": {
    "dev": "src/environments/environment.ts",
    "prod": "src/environments/environment.prod.ts"
}

Be sure that you've the reference to the files right.

dmartmont avatar Jul 29 '17 01:07 dmartmont

FYI - I also get this issue when referencing an invalid d.ts file. Confirmed on 1.3.0-rc.5 (which I am stuck on due to another bug)

SpiraMira avatar Aug 21 '17 06:08 SpiraMira

select: function () {
var self = this;
self.$http.post("/purchase/rfq-table", {
"username": self.options,
}).then(function(res){
var data = res.body.result;
self.tabledata = [];
if(data.length > 0){
data.forEach(function(val) {
self.tabledata.push({
"data": [
val.name,
val.schedule_date,
],
"url": "/purchase/RQ/"+val.id,
});
});
console.log(self.tabledata);
}
//self.options =res.body.data;
},function(err){
alert(err);
});
},

Uncaught (in promise) TypeError: Cannot read property 'length' of undefined

i dont know the error please help me out this.

unsama avatar Aug 26 '17 12:08 unsama

@unsama simply do: if(data && data.length > 0)

elvisbegovic avatar Aug 26 '17 12:08 elvisbegovic

@istiti thanks

unsama avatar Aug 26 '17 13:08 unsama