Angular-cli: Error: Unexpected end of file

Created on 7 Jun 2018  Â·  15Comments  Â·  Source: angular/angular-cli

Bug Report or Feature Request (mark with an x)

- [x] bug report
- [ ] feature request

Area

- [x] devkit
- [ ] schematics

Versions

node --version
v10.3.0

npm --version
6.1.0

sw_vers
ProductName:    Mac OS X
ProductVersion: 10.12.6
BuildVersion:   16G1314

Repro steps

npm uninstall -g @angular/cli

npm cache verify

npm install -g @angular/cli

ng version
     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / â–³ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 6.0.8
Node: 10.3.0
OS: darwin x64
Angular:
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.6.8
@angular-devkit/core         0.6.8
@angular-devkit/schematics   0.6.8
@schematics/angular          0.6.8
@schematics/update           0.6.8
rxjs                         6.2.0
typescript                   2.7.2

ng new foo                                                                                                                                                                                                                                                                                                        ✘ 141

The log given by the failure

Unexpected end of file.
Error: Unexpected end of file.
    at _readValue (/usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/core/src/json/parser.js:533:19)
    at parseJsonAst (/usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/core/src/json/parser.js:605:17)
    at Object.parseJson (/usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/core/src/json/parser.js:631:12)
    at getLegacyPackageManager (/usr/local/lib/node_modules/@angular/cli/utilities/config.js:179:35)
    at Object.getPackageManager (/usr/local/lib/node_modules/@angular/cli/utilities/config.js:120:38)
    at NewCommand.runSchematic (/usr/local/lib/node_modules/@angular/cli/models/schematic-command.js:74:38)
    at NewCommand.<anonymous> (/usr/local/lib/node_modules/@angular/cli/commands/new.js:77:25)
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/@angular/cli/commands/new.js:7:71
    at new Promise (<anonymous>)

Desired functionality

Properly recovered from or better error message caused by bad data in user files.
The cause was that ~/.angular-cli.json was an empty file.

Mention any other details that might be useful

I track down the error using

node --inspect --debug-brk  /usr/local/bin/ng new foo

with a break-point in /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/core/src/json/parser.js just before UnexpectedEndOfInputException call super("Unexpected end of file.")
then I realize that this file const legacyGlobalConfigPath = path.join(homeDir, '.angular-cli.json') was used and when I take a look it was empty, so the error is consistent with a bad json parsing, but at least will be good if we had the filename in the error

devkicore low investigation confusing triage #1 bufix

Most helpful comment

Often this is caused by a syntax error in angular.json

All 15 comments

Thats the issue of Angular CLI

Often this is caused by a syntax error in angular.json

@daniele-pecora thanks for the insight! It's definitely a bad DX and we should look at this.

@jjlorenzo would you check the latest CLI version and let us know if you can still reproduce the issue? We should handle parsing errors better in recent releases.

when I upgrade angular-cli version from 1.6.6 to 9, I got this issue also

I am also getting this issue. Definitely related to JSON parsing. I just don't know which file is causing the offense.

Upgrading my nodejs version from v.10 to v.12 worked for me
I'm using Windows 8.1

I got the same error, and it was due to my proxy json config (it was empty by mistake)
So check all json files managed by CLI

@jedjebari, are you using the latest stable Angular CLI version?

I just tested with an empty proxy configuration file and it's a different error, and the error is self explanatory:

> ng serve -c proxy
An unhandled exception occurred: /workspace/MY_PROJECT/proxy.conf.json: Unexpected end of JSON input
See "/tmp/ng-sNNq17/angular-errors.log" for further details.

> cat /tmp/ng-sNNq17/angular-errors.log
[error] SyntaxError: /workspace/MY_PROJECT/proxy.conf.json: Unexpected end of JSON input
    at parse (<anonymous>)
    at Object.Module._extensions..json (internal/modules/cjs/loader.js:1192:22)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at _addProxyConfig (/workspace/MY_PROJECT/node_modules/@angular-devkit/build-angular/src/dev-server/index.js:521:23)
    at buildServerConfig (/workspace/MY_PROJECT/node_modules/@angular-devkit/build-angular/src/dev-server/index.js:373:9)
    at setup (/workspace/MY_PROJECT/node_modules/@angular-devkit/build-angular/src/dev-server/index.js:113:67)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I'm able to reproduce on v9.1.7 by introducing a syntax error to angular.json. Definitely could use a better error message, as there is nothing indicating which file has the problem. Presumably other JSON files might cause similar errors.

douglasparker@basilisk (~/Source/fixit-test)
[20-05-26 13:36:04]$ ng serve
An unhandled exception occurred: Expected end of file, got ",
  "defaultProject"..." at 122:3.
See "/home/douglasparker/Temp/ng-lMGeKl/angular-errors.log" for further details.

@SchnWalter : I got the obscur error while running ng update, not ng serve.
But I was on unhappy path, because I cleared my proxy conf after running ng serve.
That should probably never happen ...

I'll try it another time to be sure

Just updated the CLI with an empty JSON proxy file in a project that contains a proxy configuration, and the update worked.

> ng update @angular/cli
Installing packages for tooling via npm.
Installed packages for tooling via npm.
Using package manager: 'npm'
Collecting installed dependencies...
Found 47 dependencies.
Fetching dependency metadata from registry...
Updating package.json with dependency @angular/cli @ "9.1.7" (was "9.1.6")...
Updating package.json with dependency @angular-devkit/build-angular @ "0.901.7" (was "0.901.6")...
Updating package.json with dependency @angular-devkit/build-ng-packagr @ "0.901.7" (was "0.901.6")...
UPDATE package.json (2669 bytes)
✔ Packages installed successfully.

And then I updated @angular/cli after introducing a syntax error in angular.json, and the update went well. So, maybe, this depends on the schematics that are being used.

Then I've tested this on a project that had the latest @angular/core@8 and @angular/cli@8 And I got this message when I've introduced the same syntax error:

>  ng update @angular/core @angular/cli
An unhandled exception occurred: Workspace config file cannot be loaded: /workspace/MY_PROJECT/angular.json
Invalid JSON character: "," at 23:14.
See "/tmp/ng-ybYDUe/angular-errors.log" for further details.

Which is very clear in terms of where the syntax error is.

angular 9 to angular 10 upgrade.I tried both node versions 10.17 and 12.18, operating system ubuntu 18.04.4
it happens with the typescript migration step
Add "Solution Style" TypeScript configuration file support. This improves developer experience using editors powered by TypeScript’s language server. Read more about this here: https://v10.angular.io/guide/migration-solution-style-tsconfig ✖ Migration failed: Unexpected end of file. See "/tmp/ng-j7kG46/angular-errors.log" for further details.

[error] Error: Unexpected end of file. at _readValue (/tmp/angular-cli-packages-OQomyr/node_modules/@angular-devkit/core/src/json/parser.js:625:19) at Object.parseJsonAst (/tmp/angular-cli-packages-OQomyr/node_modules/@angular-devkit/core/src/json/parser.js:722:17) at visitExtendedJsonFiles (/tmp/angular-cli-packages-OQomyr/node_modules/@schematics/angular/migrations/update-10/solution-style-tsconfig.js:30:32) at visitExtendedJsonFiles.next (<anonymous>) at visitExtendedJsonFiles (/tmp/angular-cli-packages-OQomyr/node_modules/@schematics/angular/migrations/update-10/solution-style-tsconfig.js:45:16) at visitExtendedJsonFiles.next (<anonymous>) at visitExtendedJsonFiles (/tmp/angular-cli-packages-OQomyr/node_modules/@schematics/angular/migrations/update-10/solution-style-tsconfig.js:45:16) at visitExtendedJsonFiles.next (<anonymous>) at visitExtendedJsonFiles (/tmp/angular-cli-packages-OQomyr/node_modules/@schematics/angular/migrations/update-10/solution-style-tsconfig.js:45:16) at visitExtendedJsonFiles.next (<anonymous>) at visitExtendedJsonFiles (/tmp/angular-cli-packages-OQomyr/node_modules/@schematics/angular/migrations/update-10/solution-style-tsconfig.js:45:16) at visitExtendedJsonFiles.next (<anonymous>) at visitExtendedJsonFiles (/tmp/angular-cli-packages-OQomyr/node_modules/@schematics/angular/migrations/update-10/solution-style-tsconfig.js:45:16) at visitExtendedJsonFiles.next (<anonymous>) at host (/tmp/angular-cli-packages-OQomyr/node_modules/@schematics/angular/migrations/update-10/solution-style-tsconfig.js:56:45) at MergeMapSubscriber.pipe.operators_1.mergeMap.inputTree [as project] (/tmp/angular-cli-packages-OQomyr/node_modules/@angular-devkit/schematics/src/rules/call.js:75:24)

UPDATE
To overcome this problem I made this step manually
1- by renaming tsconfig.json to tsconfig.base.json
2- I added tsconfig.json, tsconfig.app.json and tsconfig.spec.json
but after this fix I am facing another problem with the new tsconfig.json file
Could not resolve module '@angular/core' relative to file undefined
when I use the old one without the "files array" everything works

tsconfig.app.json has a structure different from tsconfig.json. I'd recommend you to generate a new project and copy the content of tsconfig.base.json from there.

@mgechev Thank you for ur reply. I did this I copied actually the 5 tsconfig files and still got the same problem
tsconfig.app.json- tsconfig.base.json - tsconfig.json - tsconfig.spec.json - e2e/tsconfig.json

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MateenKadwaikar picture MateenKadwaikar  Â·  3Comments

JanStureNielsen picture JanStureNielsen  Â·  3Comments

donaldallen picture donaldallen  Â·  3Comments

daBishMan picture daBishMan  Â·  3Comments

hareeshav picture hareeshav  Â·  3Comments