NPM is an important way to install front-end engineering dependency packages. Understanding the whole process of NPM is the foundation for mastering front-end engineering.

download

First, there is the download part. After the NPM install command, it will first go to the node_modules directory to find whether there is a corresponding module. If there is no corresponding module, it will download according to the zip package address in Registry.

The installation

NPM first executes the preinstall hook or, if not defined, passes the null function. Next, install the first layer of dependencies for this module, which are found and installed through the definition of dependencies in package.json. The project itself can be regarded as the root node of the whole dependency tree, and each first-level dependency tree is a sub-tree below the root node. NPM will search for deeper dependency modules from the root node of the first-level dependency tree in turn.

The module is flat

So the question is, if module A depends on React and module B depends on React, the React module will be downloaded twice or more? This was true prior to NPM3, resulting in wasted network bandwidth and wasted time. Therefore, nPM3 improves this problem by traversing all dependencies before downloading and flattening all dependencies. This means that instead of a- > C, b- > C, you need to install A, B, C. This process is called NPM-dedupe.

Lifecycle of NPM

The public,

Finally, welcome to pay attention to my wechat public number, will continue to update, be there or be square.