The introduction

Nodejs package manager NPM is almost indispensable for front-end development, such as building local development services and packaging and compiling front-end code. In front end development, NPM install is often used to install the required dependencies. As far as the technical details are concerned, here is the node package manager NPM.

Rely on NPM install

To use NPM to manage nodeJS package dependencies, you need to provide a package.json file in the project root directory with fields related to package dependencies:

  • Dependencies: Specifies the module on which the project runs
  • DevDependencies: Specifies the modules needed for project development
  • PeerDependencies: Specifies the module and its version required by the host environment in which the current module resides

NPM install –save $package to install runtime dependent modules, NPM install –save-dev $package to install local development dependent modules.

Install dependencies for NPM install dependencies for NPM install

  • The package specified in Dependencies is installed only in two cases:
    • In an area containingpackage.jsonTo executenpm install
    • Execute in any directorynpm install $package
  • The package specified by devDependencies is installed as follows:
    • In an area containingpackage.jsonTo executenpm install;but, the implementation ofnpm install --productionThis command does not install the package specified in devDependencies
    • performnpm install $package --devWill install the corresponding dependency package;but, the implementation ofnpm install $packageThe package specified in devDependencies is not installed

So:

NPM installs the dependencies specified in devDependencies in package.json. DevDependencies are not installed.

NPM install $package NPM install $package NPM install $package NPM install $package NPM Install $package

If the corresponding package is not installed in the host environment or the installed version does not meet the requirements, the NPM displays an error warning during the installation.

Differences between NPM2 and NPM3 + installation dependencies

When NPM installs dependencies, it downloads them to the current node_modules directory. Does each package have its own node_modules after installation? This, in turn, involves different versions of NPM that have different directory structures that depend on packages.

Npm2 depends on installation

Npm2 dependencies are easy and straightforward to install. You can download and populate the local directory structure according to the tree of package dependencies. That is, each package will organize its dependencies into the node_modules directory where the current package resides.

The reason for this design of NPM2 may be a quote from article [2] :

Because NPM is designed to take into account the complex relationship between the versions of packages that depend on them. Multiple versions of the same package can occur due to dependencies, simply populating the structure ensures uniform behavior and structure for both installation and removal.

In this way, modules A and C in A project App both depend on B, and the following directory structure will be generated regardless of whether B is of the same version:

Is obvious:

This dependent organization structure, while simple to achieve multi-version compatibility, may result in deep nesting of directory structures, and is likely to cause a large number of redundancy problems for the same modules.

Npm3 + depends on installation

Npm3 takes a “flat structure” approach to organize the directory structure of dependent packages. NPM install:

When encountering a new package, the package will be placed in the first level directory. If it encounters a package that already exists in the first level directory, the version of the package will be determined first. If the version is the same, the package will be ignored.

Take a look at the comparison results in the example above:

In this way, NPM3 + partially solves the nPM2 pain point by adopting this flat structure.

Why is it a partial solution? Npm3 + does not perfectly solve problems in NPM2 and in some cases even degrades to nPM2 behavior.

For example, the project App relies on modules A, C, D and E, where A, C and D depend on module B v2.0 and E depends on module B v1.0. The generated NPM3 structure is as follows

It can be seen that B, C and D modules contain B V2.0, which are dependent on each other, and there is code redundancy.

Can the problem of code redundancy be solved? On the premise that the module B dependent on module E is upgraded to V2.0, we can redirect all the second-level dependent module B V2.0 to the first-level module B through NPM dedupe, as shown in the following figure:

Node_modules Path lookup

The structure of nPM2 and NPM3 dependency packages is different. How to find the corresponding dependency package, such as the project access Webapck dependency package:

const webapck = require('webpack')Copy the code

So how does NodeJS find the Webpack module? This is the pathfinding problem involving dependency packages. Details are as follows:

If the argument passed to require() is not a nodejs core module, it does not start with /,.. / or./, nodeJS will try to load the module from its node_modules folder, and load the js file from the module’s package.json. If not, move up one level to the root of the file system.

For example, suppose that in/home/wonyun/projects/foo js file called the require (‘ bar. Js), then the nodejs find its position in the order as follows:

  • /home/wonyun/projects/node_modules/bar.js
  • /home/wonyun/node_modules/bar.js
  • /home/node_modules/bar.js
  • /node_modules/bar.js

If the root directory of the file system is traced and the dependency is not found, nodeJS will report an error that the corresponding module cannot be found.

For the record, the above pictures are illustrated using the pictures in reference [2].

reference

3, NPM && nPMscript &&gulp&&webpack 4, What’s the difference between NPM &&gulp&&webpack 4, devDependencies and peerDependencies in npm package.json file?