npm failed randomly while installing packages. It didn’t matter which packages or if running on Debian itself or inside virtual machines using Ubuntu (vagrant). Upgrading npm to the latest version ( sudo npm install -g npm@latest ) and clearing npm’s cache ( npm cache clean ) didn’t help either.
After examining the downloaded temporary packages in /tmp I realized that they were corrupt. The corrupt packages changed randomly on every download attempt. Changing the temporary directory from /tmp to another location did not resolve the issue. Every user on the machine was affected by the problem.
Since the issue occured on every platform for every user, inside vagrant-VMs and even on different machines (Laptop, Desktop-PC, …) it was soon clear that the solution probably had nothing to do with npm.
After weeks of troubleshooting without any result I realized that the problem only ocurred when running the command at home. The command executed fine when my Laptop was inside other networks. This led me to the conclusion that the network configuration was the cause of all problems. Since all machines received their settings via DHCP my first try was to manually define the DNS settings.
Et voilà: Everything worked as expected.
Change the DNS servers you use. You’re probably using the default servers provided by your ISP. There are plenty of other public DNS servers out there. For a quick test you might even use the ones operated by Google: 22.214.171.124 and 126.96.36.199 .