What is Pnpm? How does it compare to npm/yarn?

Tram Ho

The article introduces an overview of pnpm, the advantages and disadvantages of this package manager. In the future, I will write an article to demonstrate the process of migrating from npm/yarn to pnpm

image.png

What is Pnpm?

  • According to the pnpm.io homepage, this phrase stands for “performant npm”, which translates into Vietnamese as: “high performance npm”. Just by hearing the name, you already know what the purpose of pnpm is ( for those of you who don’t know, npm stands for “node package manager” ).
  • Fun fact that the author of pnpm developed this tool at a time when yarn didn’t exist. Maybe that’s why the author named it pnpm .

What are the advantages of Pnpm?

Save memory

Assuming you have 10 projects on your machine, if you use npm , you will have a total of 10 node_modules folders that are heavier than a cosmic black hole.

But with pnpm it’s different, pnpm introduces a place called content-addressable store as you can see in the image below.

image.png

(source: https://pnpm.io/motivation#saving-disk-space)

As you can see, pnpm doesn’t save the package to node_modules folder, but rather to content-addressable store (you can think of it as a global store). So, in node_modules of projects using pnpm, the packages will actually symlink to the real path in the global store.

As a result, we reduce the need to download the same packages over and over when init a new project. That’s why pnpm saves more memory than npm/yarn

pnpm also has an offline mode like yarn, because the packages are saved in the global store, so it is completely accessible in offline network conditions.

Fast installation speed

Here is the chart of pnpm when installing the dependency (ie you will use the pnpm install command)

image.png

And here is the chart of npm, yarn

image.png

Easy to see, pnpm has a faster installation speed than yarn, npm thanks to the absence of blocking time between steps. This is due to the difference between the approach of pnpm and npm and yarn

The node_modules directory is non-flat

First of all, why is npm moving towards flat node_modules?

Going back in time, before npm version 3 came out, at this point, node_modules in npm is still in non-flat form. As the example below: (note that inside foo there is also a node_modules directory containing the dependency bar )

This approach has raised problems such as:

  • There is a problem with long directory path on windows operating system, because the package creates a dependency tree that is too “deep”
  • Packages are duplicated in many places, because it’s the depenency of other packages —-> you imagine node_modules is already very heavy, if this issue is not resolved how terrible it will be

So to solve this problem, npm decided to flat node_modules. After npm version 3, the node_modules directory structure will become this:

As you can see, though bar is a dependency of foo , not of project. But bar is still set to the same level as foo . Personally, I find flat like this to make the node_modules confusing because there are too many dependencies

In everyday life, when a problem is born, there will be many solutions. And pnpm’s workaround is not the same as npm’s. Instead of flat like npm, pnpm keeps the non-flat structure and still solves the problems above.

Also 2 packages foo and bar , when using pnpm, we will have the node_modules directory as follows:

As you have seen above,

  • Package foo is just a symlink to .registry.npmjs.org/foo/1.0.0/node_modules/foo . This is still ok, because when executing Node will find the real path of the package and run.
  • Package foo still contains its dependency, bar in the form of a symlink. And the special thing is that foo doesn’t have node_modules inside, this way foo’s dependency tree won’t be as “deep” as it was in npm before v3.

At first glance, the structure may seem complicated, but in large projects you will find this structure will be clearer than npm/yarn.

Disadvantages of pnpm

  • Not all packages natively support pnpm , so if your project is using npm/yarn , there will be some problems with migration (I will write another article to explain this more clearly)
  • Currently, I only see that pnpm has such a disadvantage, you can add it to me in the comment section, thank you

Setting

You can read more here: https://pnpm.io/installation

As simple as installing yarn

The commands are similar to npm or yarn so you are free to explore.

Also, if you wonder about content-addressable store that I mentioned above, you can use the following command to find out the store’s path:

Showcase

pnpm is used by quite a few large companies, this can also be considered as proof of the effectiveness of pnpm (you can see more here https://pnpm.io/users )

image.png

End

Thanks for reading this far, as you can see pnpm is a pretty effective tool for package management for node. Try pnpm to enjoy but its advantages bring.
The above article was referenced by me from the sources below, if you find this article ‘familiar’, you must have read the articles below

References

Share the news now

Source : Viblo