90

Building the application after having upgraded dependencies to Angular 9 (and having performed the 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 configuration to ignore any that you are not using.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kay-1234
  • 901
  • 1
  • 5
  • 5

29 Answers29

101

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

It would be important to remark that the development and production machines were running well.

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

  1. in file tsconfig.json, in angularCompilerOptions have set ("enableIvy": false)
  2. updated Node.js to the latest version (executing node -v returned v14.3.0)
  3. have deleted the node_modules folder folder 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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Florin Filip
  • 1,224
  • 1
  • 7
  • 6
52

In my particular case, the @angular-devkit/build-angular was updated to "^0.1001.2" in my package.json file after running the npm audit fix. (This version seems to belong to Angular 10, instead of the local projects' Angular version (v9.1.7))

After reverting this change, everything started working again:

"@angular-devkit/build-angular": "~0.901.6"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bas Que
  • 671
  • 6
  • 5
26

Using terminal/cmd,

First step:

ng update @angular/cli @angular/core

Second step:

ng update

Third step:

ng serve

It fixed mine.

Note

Your custom CSS files in the Bootstrap folder might get deleted. Back them up before and place them back after the update!


Global Solution

With solution above you need to do the "First step" on each project. In case you wanted to upgrade your Angular CLI/Core versions globally for all feature projects, upgrade your Angular version with this command:

npm i -g @angular/cli

Then run the Second and Third steps above.

Reza Taba
  • 1,151
  • 11
  • 15
21

This issue is because you might be running your application 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 folder folder. And once you turn on you machine and are trying to start the application again, Angular CLI will check this lock file and try 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 this error.

Solution 1. Remove folder node_modules and do npm install

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jijo Cleetus
  • 2,679
  • 1
  • 11
  • 17
17

Try to remove the node_modules folder and run install again:

rm -rf node_modules
npm install

For me it has solved the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jacek Koziol
  • 303
  • 1
  • 3
  • 8
  • 2
    You do not have to remove the whole `node_modules`, `node_modules/@angular` folder is enough. But, anyway, do not forget to `npm i`. – Eugene P. May 06 '21 at 11:46
10

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

Ekaterina Tokareva
  • 813
  • 11
  • 11
8

In my case, adding enableIvy: false didn't solve the issue, which was with the Angular Language Service Extension for Visual Studio Code (it looks like it's not actually taking the parameter into account).

The solution has been to downgrade the extension version. (v12.0.0 --> v11.2.14. See here to know how).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Patrick M.
  • 750
  • 3
  • 10
  • 21
5

Setting "enableIvy": false in file tsconfig.json does fix that particular error. Ivy is supposed to be usable already.

I found this in clement911's answer to Error after updated to version 9 #36060, and it worked for me after a lot of testing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Meir
  • 994
  • 12
  • 25
4

I had to go into my tsconfig.spec.json file and add

"angularCompilerOptions": {
    "enableIvy": false
}

It was already in my tsconfig.json file.

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). I found good instructions here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3
  • I removed tslib.js from file package.json
  • Removed file package-lock.json
  • Deleted the node_modules folder folder
  • npm i
  • ng serve

It worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AbhijithKA
  • 76
  • 4
3

Well, in my case I was trying to run an Angular project of an older version with a newer version of Node.js. I did this:

Removed all the existing nodule modules.

rm -rf node_modules

Updated Node.js to the latest:

npm update
npm install

Updated the Angular project version to the latest

ng update @angular/core

Updated the project cli to the latest

ng update @angular/cli

Now start the project

ng-serve
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saideep Ullal
  • 221
  • 3
  • 16
  • I would be VERY HESITENT to delete `node_modules`, at least rename it to a bkup until you figure out that you solved everything. `mv node_modules ~/node_modules.bkup` – A Kareem Oct 10 '21 at 13:21
2

Try to remove angular-font-awesome and run install again:

npm uninstall angular-font-awesome
ng add @fortawesome/angular-fontawesome@0.6.0

The problem is solved after I removed fontawesome and now Material Icons is my choice for icons.

But I should say thank you for Font Awesome for many years of free awesome icons :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

in that case version mismatch.. check first version node JS and angular JS

then write : npm install => : ng serve

if have port issue : ng server --port 4201

1

I got this and other bugs because I tried to update some marginal libraries of an Angular 9 application. The errors that subsequently appeared during compilation were senseless in the sense that they did not help to correct the error.

The solution by deleting "node_modules" and "npm i" does not work, because even though I have the original package.json file, "npm i" still installs different versions of the libraries—not the original ones. What exactly were the libraries in folder node_modules folder originally is not known.

The only solution that helped was to restore the node_modules folder from backup.

Otherwise, if you need to update the libraries in the application, take a vacation and prepare some antidepressants, but it still may not help in a matter of days to solve meaningless errors after upgrading the libraries.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bmi
  • 652
  • 2
  • 10
  • 17
1

There is not a situation where disabling Ivy is the best solution.

For Windows users:

npm i rimraf -g
rimraf node_modules
npm i
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan
  • 3,893
  • 5
  • 46
  • 77
1

For older Angular versions, I managed to fix this through Visual Studio extensions.

Angular CLI: 6.1.5
Node: 8.11.4

Visual Studio, Angular extension

Angular view engine

ExtensionsAngular Language Service(v12.0.2)Extension Settings

  • Under Workspace, put a check on "User Legacy View Engine language service".
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rey
  • 51
  • 3
  • Re *"Visual Studio"*: Don't you mean *"[Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code)"*? They are two different things. – Peter Mortensen Aug 31 '22 at 22:47
1

I've had this issue and none of the above answers helped.

My solution:

  • navigate to root ng project folder
  • run cmd command: ./node_modules/.bin/ngc tsconfig.json
  • this exposed a real error: one of the component *.spec.ts files (test files) had an error (I removed a dependency earlier). This wasn't surfaced by default when building or trying VS Code debugging.
  • After fixing the error, both VS Code debugger and ./node_modules/.bin/ngc tsconfig.json command succeeded
Duck Ling
  • 1,577
  • 13
  • 20
0

I had a similar problem.

In my case, updating Node.js to a new version helped.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kyselm
  • 300
  • 3
  • 11
0

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Haithem KAROUI
  • 1,533
  • 4
  • 18
  • 39
0

In my case, I installed the angular-font-awesome package from npm and then Bootstrap. It might got conflicted, but I just removed the previously installed angular-font-awesome and then did a ng build.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Satish Patil
  • 438
  • 5
  • 15
0

I had this issue when I ran ionic build while the project built just fine with my colleagues. So we ran ng -v to compare our packages. It turns out I had higher versions of @angular-devkit/build-angular, @angular-devkit/build-optimizer and @angular-devkit/build-webpack . Each of them was version 0.1001.2, and our project was running with 0.901.8.

So I ran npm uninstall @angular-devkit/build-angular 0.1001.2 to uninstall it, and npm install @angular-devkit/build-angular 0.901.8 to downgrade. On running ng -v again, the other two had downgraded as well.

Finally, ionic build was a success! We lived happily thereafter, till we run into different problems. Working packages for our project

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

"prettier": "2.2.1". Remove this, as it is interferes with semantics.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
samar
  • 1
  • 1
  • 1
    That is a bit terse. Can you elaborate, please? E.g., remove from what? What do you mean by *"interferes with semantics"*? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/67328394/edit), not here in comments (********************** ***without*** ************ "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 31 '22 at 22:39
0

For me it turned out we had used the package-lock.json stated package versioning for a while, and when I tried to delete it due to merge conflicts and recreated it, the error happened. When going back to the old package-lock.json, the problem was gone. So I started searching:

  • comparing changes between both
  • checking if problem appears in production, too - then it almost can't be a dev dependency
  • sorting out improbable and probable dependencies

I ended up at the Angular dependencies, more exactly at the @angular/material-moment-adapter having received an update (as we allow it in the package.json) from version 11.2.0 to 11.2.12.

seawave_23
  • 1,169
  • 2
  • 12
  • 23
0

In my case, the problem was that my server did not have enough RAM (1 GB), which seems not be enough to process a build. After increasing it to 2 GB, the problem was solved.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zronn
  • 1,026
  • 8
  • 26
0

For those who are using Visual Studio Code 1.62 or later. None of those answers helped me.

I updated Visual Studio Code yesterday (2021-10-11) and for some reason my Angular 9 project started to show "NGCC failed". I downgraded Visual Studio Code to 1.61.2 and everything came back to normal.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Unfortunately, none of the previous answers worked.

Downgrading typescript (4.8 to 4.6 in my case) resolved the issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • *How* did you downgrade? Please respond by editing your answer (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Oct 07 '22 at 13:14
-2

This only woked for me del node_modules npm install enableIvy:false was not support who uses angular v12 or above

Shankar
  • 17
  • 6
-6

Replace file tsconfig.json by the below JSON content:

{
  "compileOnSave": false,

  "compilerOptions": {
     "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sher Singh
  • 497
  • 5
  • 8
  • 3
    Care to explain? – Captain Kenpachi Jan 21 '21 at 08:47
  • surely "target": "es2015" is enable proper compilation – Deunz Jul 07 '21 at 13:44
  • 2
    Hi! The reason this answer is getting downvoted by your peers, is it simply isn't helpful enough. What are the parts that help to address the unhandled exception that the OP is asking about? Explaining the reasoning behind your answer is a key part of giving a good answer. – Andrew Gray Sep 15 '21 at 15:13