Why does deleting node_modules and re-installing magically fix errors?

I’m new to JavaScript development. I’ve been a back end developer for many years, but I now need to learn JavaScript for my job.

The team I’m in have had several problems that seem to be magically resolved by deleting a load of nested node_modules folders. Is this normal in JavaScript world? I’d love to understand what’s going on here.

The latest example was this error message:

Method "simulate" is only meant to be run on a single node. 0 found instead

Again, deleting the node_modules folder in the effected component and re-running yarn install fixed the issue. I’d love to know about common problems that might cause this behaviour, and how to stop them from happening.