Introduce npm module installation mechanism, why would enter npm install the corresponding module can be installed automatically?

1. npm module mounting mechanisms:

  • Issue npm installthe command
  • Whether there is a specific module in the directory inquiry node_modules
    • If there is no longer re-install
    • If there
      • npm URL to the registry query module compressed package
      • Download the archive, stored in the root directory of the .npmdirectory
      • Extracting archive into the current project node_modulesdirectory

2. npm implementation principle

After entering npm install command and Qiaoxia Enter, go through the following stages (with npm 5.5.1 for example):

  1. The implementation of the project itself preinstall

If the current project npm defined preinstall the hook this time will be executed.

  1. First floor dependency determination module

First of all need to do is to determine the project dependent on the first floor, that is, dependencies and devDependencies property directly specified module (assuming no added npm install parameters).

Project itself is a whole grain-dependent root of the tree, each module is dependent on the first floor of a sub-tree below the root node, npm will open a multi-node process began to gradually find a deeper level from each module depend on the first floor.

  1. Acquisition module

Acquisition module is a recursive process, divided into the following steps:

  • Acquisition module information. Before downloading a module, we must first determine the version, it is often because package.json is semantic version (semver, semantic version). At this point if there is the module version information Description File (npm-shrinkwrap.json or package-lock.json) you can directly take, if not get from the warehouse. Such as version packaeg.json in a package that ^ 1.1.0, npm will go to the warehouse to obtain the latest version of the accord 1.xx form.
  • Acquisition module entity. Previous'll get to the archive module address (resolved field), npm will check the local cache with this address, the cache has to take a direct, if there is no download from the warehouse.
  • Find the module depends, if there are dependent on the return to step 1, if not then stop.
  1. Flattening module (as dedupe)

Acquired in the previous step is a complete dependency tree, which may contain a large number of repeat modules. A module depends on such loadsh, B module also depends on lodash. Will depend strictly in accordance with the tree structure installed in npm3 before, it will cause the module redundancy.

From the beginning npm3 default joined the process of a dedupe. It will traverse all nodes below the root node in the module one by one, i.e. the first layer node-modules. When the module was found duplicate, it is discarded.

It should be a definition of the repeat modules, which refer to the same module name and semver compatible. Each period corresponds semver allowable range version, if the version of two modules to allow a range of intersection exists, then it can obtain a compatible version, and the version number need not be identical, which can make more redundant modules are removed in the process dedupe .

For example module dependency lodash @ foo the node-modules ^ 1.0.0, bar module dependency lodash@^1.1.0, ^ is a compatible version 1.1.0.

When foo rely lodash @ ^ 2.0.0, bar dependent lodash@^1.1.0, then according to the rules semver, both of which there is no compatible version. It will be a version on node_modules, and the other remains dependent on the tree.

For example, assume that this was originally a dependency tree:

node_modules
-- foo
---- lodash@version1

- bar
---- lodash @ version2

Version1 version2 assumptions and are compatible version, after dedupe will be the following forms:

node_modules
-- foo

-- bar

- lodash (reserved version compatible version)

Suppose version1 version2 and non-compatible version, the version remains in the back of the dependency tree:

node_modules
-- foo
-- lodash@version1

- bar
---- lodash @ version2

  1. Installation Module

This step will update the project node_modules, and execution module in the life cycle of a function (in the order preinstall, install, postinstall's).

  1. The implementation of the project life cycle itself

If you define the current project npm hook this time is executed (according to install, postinstall, prepublish, prepare the order).

The final step is to generate or later profile, npm install process is complete.

Reference http://www.ruanyifeng.com/blog/2016/01/npm-install.html

https://www.bbsmax.com/A/qVdemmnEdP/

https://www.zhihu.com/question/66629910

Guess you like

Origin www.cnblogs.com/wangxi01/p/11202584.html