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

Adding missing environment file fixed this form me.

ptrzyna avatar Aug 29 '17 16:08 ptrzyna

Just a comment to help those that find this page when getting the issue. If I rename or move files while in VS Code and ng serve is running, I get the error. Closing/Opening VS Code fixes the issue.

PooperPig avatar Sep 19 '17 04:09 PooperPig

I had this issue when i deleted a file and forgot to recall an ng serve .

AsmaGargouri avatar Sep 26 '17 10:09 AsmaGargouri

I fixed this issue by making sure the names in .angular-cli.json for environment files were pointing to the right place.

Eg. I had:

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

and should have had:

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

It was erroring on finding the right file (as described in adding the log statement to the typescript source above), but it was pointing at the correct file name, which was odd.

simonv3 avatar Sep 26 '17 17:09 simonv3

I got this similar issue when I moved one file to another directive during watch mode (ng serve). After doing ng build the issue got resolved!

roopeshchinnakampalli avatar Oct 04 '17 06:10 roopeshchinnakampalli

Even i came across with this issue but got it resolved by doing ng build

ranjith11rp avatar Oct 05 '17 03:10 ranjith11rp

this issue happens again after moving a /moduleA into another r folder /moduleB or I don't know but one of my file add ///<reference path="../../../../../node_modules/@angular/core/src/metadata/lifecycle_hooks.d.ts"/>

elvisbegovic avatar Oct 11 '17 13:10 elvisbegovic

@simonv3 : That was my problem too. Thx

haidaraM avatar Oct 22 '17 13:10 haidaraM

Adding missing environment folder also fixed this problem for me.

brunoliveira8 avatar Nov 01 '17 19:11 brunoliveira8

Please I'm having the same error and i have been trying to resolve the issue for he past 3 days now with any solution.

Node version v8.8.1 npm version 5.4.2

Here is the error message i'm getting

TypeError: Cannot read property 'length' of undefined at createSourceFile (E:\web\bsv.app\ng4\node_modules\typescript\lib\typescript.js:16551:110) at parseSourceFileWorker (E:\web\bsv.app\ng4\node_modules\typescript\lib\typescript.js:16483:26) at Object.parseSourceFile (E:\web\bsv.app\ng4\node_modules\typescript\lib\typescript.js:16432:26) at Object.createSourceFile (E:\web\bsv.app\ng4\node_modules\typescript\lib\typescript.js:16282:29) at new TypeScriptFileRefactor (E:\web\bsv.app\ng4\node_modules@angular\cli\node_modules@ngtools\webpack\src\refactor.js:34:35) at Object.resolveEntryModuleFromMain (E:\web\bsv.app\ng4\node_modules@angular\cli\node_modules@ngtools\webpack\src\entry_resolver.js:105:20) at AotPlugin._setupOptions (E:\web\bsv.app\ng4\node_modules@angular\cli\node_modules@ngtools\webpack\src\plugin.js:152:50) at new AotPlugin (E:\web\bsv.app\ng4\node_modules@angular\cli\node_modules@ngtools\webpack\src\plugin.js:27:14) at _createAotPlugin (E:\web\bsv.app\ng4\node_modules@angular\cli\models\webpack-configs\typescript.js:55:12) at Object.exports.getNonAotConfig (E:\web\bsv.app\ng4\node_modules@angular\cli\models\webpack-configs\typescript.js:71:19) at NgCliWebpackConfig.buildConfig (E:\web\bsv.app\ng4\node_modules@angular\cli\models\webpack-config.js:29:37) at Class.run (E:\web\bsv.app\ng4\node_modules@angular\cli\tasks\serve.js:43:98) at check_port_1.checkPort.then.port (E:\web\bsv.app\ng4\node_modules@angular\cli\commands\serve.js:114:26) at at process._tickCallback (internal/process/next_tick.js:188:7)

package.json file

{ "name": "name", "version": "1.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": { "@agm/core": "1.0.0-beta.0", "@angular/animations": "4.3.2", "@angular/cdk": "2.0.0-beta.8", "@angular/common": "4.3.2", "@angular/compiler": "4.3.2", "@angular/core": "4.3.2", "@angular/flex-layout": "2.0.0-beta.8", "@angular/forms": "4.3.2", "@angular/http": "4.3.2", "@angular/material": "2.0.0-beta.8", "@angular/platform-browser": "4.3.2", "@angular/platform-browser-dynamic": "4.3.2", "@angular/router": "4.3.2", "@angular/service-worker": "^1.0.0-beta.16", "@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.5", "@swimlane/ngx-datatable": "9.3.0", "@types/file-saver": "0.0.1", "angular-calendar": "0.17.0", "angular-sortablejs": "^2.0.6", "angular-tree-component": "3.8.0", "angular2-jwt": "^0.2.3", "angular2-moment": "^1.7.0", "angular2-recaptcha": "^0.6.0", "angular4-paystack": "^1.2.1", "chart.js": "2.6.0", "ckeditor": "4.6.0", "core-js": "2.4.1", "css-element-queries": "^0.4.0", "d3": "^3.5.17", "dragula": "3.7.2", "easy-pie-chart": "2.1.7", "file-saver": "^1.3.3", "font-awesome": "4.7.0", "hammerjs": "2.0.8", "jquery": "2.2.4", "leaflet": "^1.0.2", "leaflet-map": "0.2.1", "material-datetime-picker": "^2.4.0", "moment": "^2.15.0", "ng2-breadcrumb": "0.5.6", "ng2-charts": "1.6.0", "ng2-ckeditor": "1.1.8", "ng2-device-detector": "^1.0.0", "ng2-dragula": "1.3.1", "ng2-file-upload": "1.2.1", "ng2-file-uploader": "^0.1.4", "ng2-translate": "5.0.0", "ng2-validation": "4.2.0", "ng2modules-easypiechart": "^0.0.4", "ng4-social-login": "^1.3.3", "ngx-color-picker": "^4.4.0", "ngx-perfect-scrollbar": "^2.0.1", "ngx-quill": "^1.5.1", "normalize.css": "^5.0.0", "nvd3": "^1.8.5", "quill": "^1.2.6", "rxjs": "5.4.2", "screenfull": "^3.3.2", "sortablejs": "^1.6.1", "ts-helpers": "^1.1.2", "zone.js": "0.8.16" }, "devDependencies": { "@angular/cli": "1.3.2", "@angular/compiler-cli": "4.3.2", "@types/d3": "^3.5.17", "@types/jasmine": "2.5.52", "@types/jquery": "3.2.0", "@types/node": "7.0.8", "@types/nvd3": "^1.8.33", "codelyzer": "3.0.1", "html-webpack-plugin": "^2.30.1", "jasmine-core": "2.6.3", "jasmine-spec-reporter": "4.1.0", "karma": "1.7.0", "karma-chrome-launcher": "2.1.1", "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.0.6", "tslint": "5.4.3", "typescript": "2.4.0", "webpack": "^3.8.1", "webpack-dev-server": "^2.9.4" } }

.angular-cli.json file

{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "project": { "version": "1.0.0", "name": "BulkSMSVilla" }, "apps": [ { "root": "src", "outDir": "dist", "assets": [ "assets" ], "index": "index.html", "main": "app/main.ts", "polyfills": "polyfills.ts", "test": "test.ts", "tsconfig": "tsconfig.app.json", "testTsconfig": "tsconfig.spec.json", "prefix": "ms", "styles": [ "assets/scss/style.scss", "../node_modules/quill/dist/quill.snow.css", "../node_modules/leaflet/dist/leaflet.css", "../node_modules/angular-calendar/dist/css/angular-calendar.css", "../node_modules/@swimlane/ngx-datatable/release/index.css", "../node_modules/@swimlane/ngx-datatable/release/themes/material.css", "../node_modules/nvd3/build/nv.d3.css", "../node_modules/font-awesome/scss/font-awesome.scss", "assets/fonts/data-table/icons.css", "../node_modules/dragula/dist/dragula.css" ], "scripts": [ "../node_modules/jquery/dist/jquery.min.js", "../node_modules/hammerjs/hammer.js", "../node_modules/sortablejs/Sortable.js", "../node_modules/chart.js/dist/Chart.bundle.min.js", "../node_modules/easy-pie-chart/dist/easypiechart.js" ], "environmentSource": "environments/environment.ts", "environments": { "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } } ], "e2e": { "protractor": { "config": "./protractor.conf.js" } }, "lint": [ { "project": "src/tsconfig.app.json" }, { "project": "src/tsconfig.spec.json" }, { "project": "e2e/tsconfig.e2e.json" } ], "test": { "karma": { "config": "./karma.conf.js" } }, "defaults": { "styleExt": "scss", "component": {} } }

Please help.

ghost avatar Nov 02 '17 21:11 ghost

@kelvinrolex Most people are seeing this error when there is a filepath configured in the .angular-cli.json that the cli can't find in your project directory. Quite often it appears to be a missing environment.ts but It's going to be a case of checking each file in turn and making sure the filepath is correct.

simonneedham avatar Nov 02 '17 22:11 simonneedham

Thanks so much @simonneedham, problem solved, in my own case, i compared the .angular-cli.json file with an old backup i did and i noticed the "main": "app/main.ts", line is having app so i removed the app and ng serve and it started working again.

ghost avatar Nov 03 '17 03:11 ghost

npm update will help you...

coderben2017 avatar Nov 03 '17 07:11 coderben2017

@kelvinrolex Cool,. I think that instead of barfing with 'Cannot read property 'length' of undefined' the CLI should be throwing an error telling you which filepath/file it couldn't find in .angular-cli.json.

simonneedham avatar Nov 03 '17 07:11 simonneedham

@simonneedham yeah, the CLI wasn't specific at all, I spent 3 days battling with that error without any success until you said I should look into .angular-cli.json. Thanks once more.

ghost avatar Nov 03 '17 10:11 ghost

same issue here, I upgraded a library (Msal JS) in a branch, then reverted back to master, without running yarn. The tip to add a console.log in typescript.js was brilliant! Thanks @calebkiage

ghost avatar Nov 13 '17 08:11 ghost

I get same problem when I run "ng build --aot" but I don't wait it finish ,just type "ctrl + c" stop it I fix this issue when I delete my project and checkout from my svn place and run "npm i"

z83u83vul3g avatar Nov 22 '17 06:11 z83u83vul3g

Everyone look at the lines below this one: https://github.com/angular/angular-cli/blob/4b0c7a35359effc1d93cd3bb5ddc5c441b3cf154/packages/%40ngtools/webpack/src/angular_compiler_plugin.ts#L157 That basePath variable is not used anywhere, and the actual config basePath gets reset to the current directory anyway... and later on in the execution jumping through some unintelligible code, ends up here: https://github.com/angular/angular-cli/blob/4b0c7a35359effc1d93cd3bb5ddc5c441b3cf154/packages/%40ngtools/webpack/src/refactor.ts#L107 source || _host.readFile(fileName) is undefined. So basically if you move the files you wanna compile somewhere outside you get this. Can someone explain this nonsense code here?

gizm0bill avatar Nov 28 '17 13:11 gizm0bill

image the filename passed to createSourceFile function has wrong name ,

Its taking adventureimage.ts but actually it is AdventureImage.ts (capital A and I), Renaming the file name to all small solves the problem.

vivekfyi avatar Nov 28 '17 18:11 vivekfyi

this is very weird, but i got it working similar to the above. You just need to make sure every array and key has been filled with a value that exists, even when you might not use that key.

    {
      "name": "core",
      "root": "core",
      "outDir": "dist",
      "assets": [],
      "index": "../src/index.html",
      "main": "../src/main.ts",
      "polyfills": "../src/polyfills.ts",
      "test": "../src/test.ts",
      "tsconfig": "../src/tsconfig.app.json",
      "testTsconfig": "../src/tsconfig.spec.json",
      "prefix": "core",
      "styles": [],
      "scripts": [],
      "environmentSource": "../src/environments/environment.ts"
    }

actually, in the end I modified the test.ts, since there's a context I don't know how to override.

  const context = require.context('../core', true, /\.spec\.ts$/);

windmaomao avatar Nov 28 '17 19:11 windmaomao