common-shakeify icon indicating copy to clipboard operation
common-shakeify copied to clipboard

What is the "Could not redupe module ..." actually mean?

Open ceremcem opened this issue 3 years ago • 3 comments

I received an error:

browserify : Could not redupe module /path/to/scada.js/node_modules/moment/moment.js

What does it mean? What should I do?

ceremcem avatar Aug 05 '20 15:08 ceremcem

Browserify internally dedupes modules: if there are two identical files in the build, it will replace one of them with a stub that references the other. For tree shaking, this causes an issue, because the stub file does not appear to have any exports just by looking at the source. common-shakeify works around it by temporarily un-deduping the module, that is by replacing the stub with the original source.

This "reduping" can fail if browserify has deduped a module, but the original source cannot be retrieved.

Looking at the code I can see a potential cause of this … common-shakeify expects the original source to be earlier in browserify's build pipeline than the deduped stub module. If some plugin sorted the pipeline before common-shakeify gets to it, it's possible that the order is reversed.

I think the only feasible way to work around it is by checking if you have multiple copies of moment in your dependency tree. You can try to use npm dedupe to hopefully reduce it to one. Then browserify does not need to do the deduping at all. Also, you can run browserify with the --no-dedupe flag, though this may cause bundle sizes to increase quite a bit.

goto-bus-stop avatar Aug 14 '20 06:08 goto-bus-stop

Thanks. Now I understand more what browserify does under the hood.

I think the only feasible way to work around it is by checking if you have multiple copies of moment in your dependency tree.

Yes, I know there is. I can remove it by editing my code (it also might be a code cleanup) but I feel like it shouldn't be the way to go. I'll try this option anyway.

This "reduping" can fail if browserify has deduped a module, but the original source cannot be retrieved.

I can't imagine how this step fails. I might take a look at this process. It might be interesting to find out this actual cause.

ceremcem avatar Aug 14 '20 16:08 ceremcem

I can remove it by editing my code (..) but I feel like it shouldn't be the way to go.

I agree. I basically thought it couldn't fail, which is why I didn't put much effort into the error message :sweat_smile:

goto-bus-stop avatar Aug 14 '20 17:08 goto-bus-stop