PNPM is a package management tool for nodeJS. The main feature is high performance NPM.

In fact, I also tried PNPM before the release of PNPM, but it failed to go further, because PNPM, as a new package management tool, is difficult to compete with both function richness and community ecology under the dominance of NPM and YARN. However, due to some problems in the optimization of internal project platform recently, I tried the built-in MONorePO of PNPM, and it can be said that the efficiency is indeed high, which means that PNPM has been transferred from YARN + LERNA to PNPM. PNPM has been implemented and implemented. In general, PNPM is really excellent, and the laters are ahead.

This article will introduce some of the features of PNPM. Why did I choose PNPM?

How fast is PNPM?

This picture is from the benchmark test on the official website. I believe that a picture is worth a thousand words. No matter what combination, THE speed of PNPM is extremely fast.

Tiling dependency management is not the only solution to node_modules

NPM is the package manager we learned from Node. In NPM V1 and V2, packages are managed through a nested structure, which is problematic, for example:

  • Dependencies cannot be shared
  • The dependency level is too deep, resulting in a long file path

Yarn uses tiling, which solves this problem and reduces node_modules’ size, but also introduces new problems:

  • Phatom Dependencies

Dependency management in YARN and NPM V3 + is tiled. Node_modules are familiar with the project, so YARN is compatible with NPM management. Tiled dependency trees cause many problems, the most classic problem is that modules can access packages they do not depend on. I can import packages that package.json doesn’t have.

For example, A depends on A in the project, A depends on B, in package.json, there is only A, but because of flat management, node_modules can see A and B at the same level:

▾ node_modules
    ▸ A
    ▸ B
Copy the code

In this way, we can directly find B through nodeJS require addressing feature. I have used this flaw before to reduce part of the dependency, but later appeared that A really does not depend on B, so I no longer use this flaw.

PNPM’s dependency management is based on the network + tiled directory structure. After PNPM install, we can see from the local project node_modules. PNPM is actually wrapped in node_modules/. PNPM /xx using symlink to soft link under node_modules.

Pnpm. IO /zh/blog/202… .

In this article, I introduced some of the file structures for PNPM’s current node_modules, such as installing a dependency called Express in a project using PNPM, You end up with a directory structure in node_modules that looks like this (omitting non-express dependent directories) :

▾ .pnpm
    ▾ [email protected]
.modules.yaml
▾ express
    ▸ lib
      History.md
      index.js
      LICENSE
      package.json
      Readme.md
Copy the code

Express does not have nodemodules. In fact, this file is just a soft connection that forms a soft connection to a second directory (similar to the shortcut we use on the desktop), so that while Node is looking for a path, You will eventually find the contents of the.pnpm directory.

In this form, PNPM does a good job of isolating packages, so there is a very strict distinction between versions of dependencies.

Monorepo support

Lerna and Yarn Workspace are the most commonly used monorepo management tools. These monorepo management tools mainly solve the problem of managing multiple packages in a warehouse:

  • The same third-party dependencies are packaged in individual packages, increasing the size of the entire project
  • Link between modules

Although the above problem is solved, it also causes more serious Phatom dependencies, and yarn and LERNA configuration is troublesome, especially the difference between independent and mixed use of the two, I think it is difficult to understand.

PNPM is a perfect solution in the Monorepo scenario, because of its design mechanism, many critical or fatal problems are solved quite effectively.

PNPM has built-in support for monorepo. You only need to create pnpm-workspace. Yaml and.npmrc configuration files in the root directory of the workspace. It also solves the problems introduced by traditional schemes.

Hard link

PNPM also uses a mechanism called Hard Link in the computer. PNPM uses symlink to find the dependency address of the corresponding virtual disk directory (.pnpm directory).

For example, there is a 1MB dependency a in the project. In PNPM, it looks like this dependency takes up 1MB of both the node_modules directory and 1MB of the global store directory (adding up to 2MB). But because the hard Link mechanism allows the same 1MB space in two directories to be addressed from two different locations, the A dependency actually only takes 1MB of space, not 2MB.

Pnpm. IO /faq#why-doe…

conclusion

There is no need to say too much about the advantages of PNPM, and the author of PNPM himself has also made efforts to improve the feature of PNPM and plan the future development direction. The natural advantages make it simple and efficient to support workspace Monorepo. The construction is very simple, and it can also be shared between projects. Nor is it tied to Git.

However, PNPM is not a silver bullet, and any new thing needs to be viewed in multiple aspects. For example, PNPM’s peerDependencies problem is also caused by its strict isolation, which is not explained here.