Recently I was looking at starting a new exploratory project when I noticed pnpm being touted as a better alternative to npm. I read through some of the documentation and have now decided to use pnpm for the project. I think this is going to come with some challenges especially if i choose to use a lot of opensource packages.
Here is a summary of my findings .. mostly from https://pnpm.io/motivation
Both pnpm
and npm
are package managers for JavaScript and TypeScript projects, but they have some differences in how they manage dependencies and store packages.
- Dependency management:
npm
: When you install packages usingnpm
, it creates a separate copy of the package for each dependency in thenode_modules
folder, even if multiple packages depend on the same version of a library. This can lead to increased disk space usage and longer installation times.pnpm
:pnpm
addresses the issue of duplicated packages by using a global store for all packages and creating hard links or symlinks in thenode_modules
folder for each project. This means that the same version of a package is stored only once on the disk, reducing disk space usage and speeding up installation times.
- Strictness:
This is probably the main advantage of pnpm.
npm
:npm
allows packages to access undeclared dependencies, which might cause hidden issues and make the project less reliable, especially when updating or changing dependencies.pnpm
:pnpm
enforces strictness by creating an isolated environment for each package, ensuring that a package can only access its declared dependencies. This can help to catch issues related to undeclared dependencies and improve the overall reliability of the project.
- Performance:
This is definitely a benefit for larger projects however it is not a big deal for most experimental projects like mine.,
npm
:npm
is generally slower when it comes to installing packages due to the duplication of dependencies and the lack of hard links or symlinks.pnpm
:pnpm
offers better performance in terms of installation speed, as it stores packages in a global store and creates hard links or symlinks in thenode_modules
folder. This leads to faster installations and less disk space usage.
- Compatibility:
I have yet to encounter any issues with compatibility with npm but it is very likelyy I will if I use outside packages. I mostkly like tokeep dependencies to a minimum due to the unreliabilityy of open source components so it may be less of an issue for me.
npm
: As the default package manager for Node.js,npm
enjoys widespread adoption and compatibility with the majority of JavaScript projects and libraries.pnpm
: Whilepnpm
is mostly compatible withnpm
, there might be cases where a project or library does not work out of the box withpnpm
due to its strictness or symlink handling. However, most projects should work without issues.
In summary , both pnpm
and npm
have their pros and cons. Depending on your sitiation , if you prioritize efficiency, strictness, and performance, pnpm
might be a better choice. However, if you value widespread compatibility and don't mind the increased disk space usage, npm
could be the right option for you.
For most professional projects I think switching to pnpm seems to make more sense.