DEV Community

negue
negue

Posted on

No template for dependency: "ConstDependency"

Over the past years, I came across so many weird issues while developing and most of them I couldn't find any mentions or articles or even any stackoverflow posts.

Yesterday I had another one of these and since I want to write more articles why not create weird issues series? Welcome to the first part of this series 😁

No template for dependency: "ConstDependency"

While trying to add a simple worker in angular, like:

const markdownWorker = () => new Worker('./markdown.worker.ts', {
  name: 'markdown',
  type: "module"
});

I got this error message.

Like I already wrote "no real results about it", just some for No template for dependency:.

I tried multiple times switching the angular version (9 rc5 / 6), which also meant deleting and reinstalling all packages.

-- still not working

I created a new project (different folder) it just works out of box.

Compared the angular.json / tsconfig / packages - copied even the app to the other folder , in that other folder everything is working fine.

Tracked down where message issue was written:
/node_modules/@angular-devkit/build-angular/node_modules/webpack/lib/ => https://github.com/webpack/webpack/blob/v4.41.2/lib/JavascriptGenerator.js#L144-L152

Added a bit more outputs to that, and the issue was that the ConstDependency-constructor couldn't be found in the dependencyTemplates-Map. But my additional logs show that dependencyTemplates did actually had a ConstDependency key.

I rewrote the const template = dependencyTemplates.get(dependency.constructor); part to check for the constructor-names instead, and look it works. 🎉

I still don't understand why the contructor of dependency wasn't the same as in the map.

Checked my node version, maybe just an older version issue, updated to 12.13.1.

I ran npm prune, and this folder /node_modules/@angular-devkit/build-angular/node_modules/webpack/lib/ was gone!, after the many.. removal and npm installs that folder was always there (throwing this issue)

Again started ng serve .... aaaand its gone!

😁


Did you had any of those weird issues lately? Would like to read about those, and also like to know if you could fix it anyway. :)

Top comments (5)

Collapse
 
spkorhonen profile image
SPKorhonen

It seems that this error is caused by local install of package webpack under @angular-devkit when there is also an independent install under host package. I would guess that the code somehow uses files from node_modules\@angular-devkit\build-angular\node_modules\webpack and node_modules\webpack somewhat haphazardly which leads to this error.

Namely the constructor object in the dependencyTemplates-map is generated form one and the dependency.constructor for the other. Thus they are not the same object and subsequently the constructor is not found from map leading to all kinds of fun and games...

It seems that ensuring identical versions of webpack in both locations and then running npm dedupe ensures that there is only one install of webpack and thus prevents this error.

Collapse
 
negue profile image
negue

Wow, that is a nice explanation :), this will probably help others easier ^^

I didn't even knew about npm dedupe , nice to know 🎉

Thank you. :)

Collapse
 
voznik profile image
Vadym P.

npm prune or npm dedupe didn't work work for me with Angular@11
found this solution on SO: stackoverflow.com/a/68462360/4115894

npm i worker-plugin --save-dev

Collapse
 
1234woodman profile image
123丶木头人

hello, i follow you advises, update node to version 12.13.1,,but i still met the problem,,can you give me more information?

Collapse
 
negue profile image
negue

Oh, I'm sorry you also having these weird issues ...

running npm prune was probably the one thing that "fixed it all" , did you ran that?

Also did you tried the latest angular version?

If those tasks don't work, I'm out of ideas, since I had this issue only once and never since then :(