Angular 9 - NGCC fails with an unhandled exception

主宰稳场 提交于 2020-08-21 06:12:35

问题


Buidling the application after having upgraded dependcies to Angular 9 (and having performed necessary code changes) throws an error:

Compiling @angular/animations : es2015 as esm2015 Compiling @angular/animations : es2015 as esm2015 Compiling @angular/core : es2015 as esm2015 Compiling @angular/core : es2015 as esm2015
Compiling @angular/core : es2015 as esm2015
Error: Error on worker #5: TypeError: Cannot read property 'fileName' of null

It then goes on to throw the below error:

Compiling @angular/core : es2015 as esm2015 Compiling @angular/compiler/testing : es2015 as esm2015 Compiling @angular/core : es2015 as esm2015
Error: Tried to write node_modules/@angular/core/core.d.ts.__ivy_ngcc_bak with an ngcc back up file but it already exists so not writing, nor backing up, node_modules/@angular/core/core.d.ts.

This error may be because two or more entry-points overlap and ngcc has been asked to process some files more than once.
You should check other entry-points in this package and set up a config to ignore any that you are not using.

Compiling @angular/core : es2015 as esm2015 An unhandled exception occurred: NGCC failed.


回答1:


This problem (NGCC failed) was encountered by me and my colleague developer on our localhost machines.

It would be important to remark that the dev and prod machines were running well.

In order to solve this problem we've followed the next steps:

  1. in tsconfig.json, in angularCompilerOptions have set ("enableIvy": false)
  2. updated the nodeJS to the last version: (executing node -v returned v14.3.0)
  3. have deleted the node_modules folder: (executing "rm .\node_modules\" [on windows], make sure it has been deleted successfully)
  4. have installed the packages: (executing npm i )
  5. here the project is ready to be built. It builds and runs without errors now.

I wish this helps you and other people in situations like this.




回答2:


Try to remove the node_modules folder and run install again:

rm -rf node_modules
npm install

For me it has solved the problem.




回答3:


Check your node.js version with node -v. And check if Angular 9 is compatible with it. Updating node.js to the latest LTS version (12.8.1) worked for me. I did this with nvm (node version manager). Found a good instruction here.




回答4:


Setting "enableIvy": false in tsconfig.json does fix that particular error. Isn't Ivy supposed to be usable already?

This answer I found at enter link description here @clement911 answer and it worked for me after a lot of testing




回答5:


I had a similar problem,

in my case updating NodeJS to new version helped.




回答6:


I generally advise people to make a manual backup of the project after every succeded deployment (locally) because, when you face this kind of problem (everything related to NGCC errors) you can just hard delete node_modules folder and restore it from you last backup. and then build again.

Sometimes, when you cannot afford updating nodeJS to the last version, this solution would be perfect.




回答7:


Adding "postinstall": "ngcc" to "scripts" in package.json helped me to fix this




回答8:


  • I removed tslib.js from package.json
  • Removed package-lock.json
  • Deleted node_modules folder
  • npm i
  • ng serve

Worked for me




回答9:


This issue is becuase you might be running your app on any port and angular ngcc was assigned with a particular process id which was specified and locked in a file called __ngcc_lock_file__ inside \node_modules\@angular\compiler-cli\ngcc. If you are doing a hard system turn off or if your os crashed, this lock file will be there in the node_modules. And once you turn on you machine and trying to start the application again, angular cli will check this lock file and trying to find the process id specified in the file. Most of the time that process id will be missing since you did a complete machine restart and it will throw the this error.

Solution

  1. remove node_modules and do npm install

  2. Smart solution - delete the lock file as below.

    Go to \node_modules\@angular\compiler-cli\ngcc find the file named __ngcc_lock_file__ delete it. Done.

Happy hacking



来源:https://stackoverflow.com/questions/61222467/angular-9-ngcc-fails-with-an-unhandled-exception