How NPM broke my server
How NPM broke my server
I own a small DigitalOcean Droplet with 1 vCPU and 1 GB of RAM, which costs me around Rs. 500 per month.
Last week, I was trying to build a documentation website with Docusaurus for a project. I pushed the project to GitHub and cloned it to my server.
Then I started installing with npm. I ran npm install
and it started installing the dependencies. After a few minutes, I got
an error saying that the server was out of memory. I was surprised
because I have only 1GB of RAM and the server was not doing anything
else. CPU and disk usage were very high.
I checked the memory usage with htop. But I couldn’t
figure it out because the server was unresponsive. I had to reboot the
server to get it back online.
I tried to install the dependencies again, but the same thing happened. I was frustrated and started searching for the issue. I found that npm uses a lot of memory to install the dependencies. I tried to increase the swap memory, but it didn’t help.
By then, I was fed up with using npm, and a few Reddit posts
mentioned that pnpm was a good alternative. So I installed
pnpm with npm install -g pnpm. It worked and I tried to
install the dependencies with pnpm install. It worked like
a charm.
Reason behind the issue
- NPM installs dependencies in a flat structure. So if a package is used in multiple dependencies, it will be installed multiple times.
- NPM uses a lot of memory to install dependencies. So if you have a small server, it will run out of memory.
- NPM uses a lot of disk space. If you have limited disk space, it will run out.
Conclusion
- If you have a small server, use
pnpminstead ofnpm. - If you have a large server,
npmworks fine. - If you want to save disk space,
yarnorpnpmare good alternatives.