Content from: https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/22
npm module installation mechanism
- Npm install command issued
- Whether there is a specific module in the directory inquiry node_modules
- If there is no longer re-install
- If not,
- npm URL to register a query module archive
- Download the archive, stored in the root directory .npm directory
- Extracting archive into the current directory project node_modules
The principle npm
After entering npm install command and Qiaoxia Enter, go through the following phases:
1. Perform project itself preinstall
If the current project npm defined preinstall hook, this time will be executed
2. The module dependency determination first floor
First, you 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.
3. Access Module
Acquisition module is a recursive process, divided into the following steps:
- Acquisition module information before downloading a module, first determine which version because package.json is often the semantic version. At this point if there is the module description file version information can take a direct, if not get from the warehouse. As in package.json version of a package is ^ 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
4. Flat module
Acquired in the previous step is a complete dependency tree, which may contain a large number of repeat modules. A module depends on such lodash, B module also depends on lodash, will be conducted in strict accordance with a dependency tree structure npm3 before, it will cause redundancy module.
From the beginning npm3 default joined the process of a dedupe. It will traverse all nodes, one by one module will be placed below the root node, which is the next node-modules, foo module depends lodash @ ^ 1.0.0, bar modules rely lodash@^1.1.0, it is compatible versions 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, a version will be put node_modules, the other remains dependent on 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_module
-- 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
The mounting module
This step will update the project node_modules, and execution module in the life cycle of a function (according to preinstall, install, postinstall order)
6. 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 last step to generate the file description or later, npm install process is complete.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Reading Ruan Yifeng "npm module in accordance with the introduction mechanism" notes
Original: http://www.ruanyifeng.com/blog/2016/01/npm-install.html
npm module Node Manager is extremely powerful. It is one of the important reasons for the success of the Node
This article describes the details of the mechanism npm module is installed, as well as how to solve the problems of slow installation speed.
A, npm install
If you wish, regardless of whether a module installed, npm should be forced to reinstall, you can use the -f or -force parameter
npm install <packageName> --force
二、npm update
If you want to update the installed modules, it is necessary to use npm update command
npm update <packageName>
It will first new version of the remote warehouse queries, then queries the local version. If the local version does not exist, or remote version is newer, it will be installed.
registry
npm warehouse module provides an inquiry service, called the registry. To npmjs.org example, its tracking URL is https://registry.npmjs.org/
The URL back to keep up with the module name, you'll get a JSON object, which is all the module version. Such as access https://registry.npmjs.org/react, you will see all versions of the module react information
It is with the following command effect is the same
npm view react # npm view 的别名 npm info react npm show react npm v react
React access https://registry.npmjs.org/react/v0.14.6 you can see the 0.14.6 version.
JSON object returned inside, there is a dist.tarball property, is the compressed version of the URL.
dist: { shasum:'2a57c2cf8747b483759ad8de0fa47fb0c5cf5c6a', tarball: 'http://registry.npmjs.org/react/-/react-0.14.6.tgz' }
The URL to download the archive and extract locally, get the source code module.
Cache directory
npm install or npm update command, after the download package from the registry, it is stored in the local cache directory.
The cache directory, by default in Linux or Mac is .npm directory under the user's home directory, in the Windows default is% AppData% / npm-cache.
By configuring the command, you can view the specific location of this directory
npm config get cache
Browse the catalog
LS ~ / .npm # or NPM Cache LS
You will see a large number of modules stored inside the storage structure is {cache} / {name} / {version}
$ npm cache ls react ~/.npm/react/react/0.14.6/ ~/.npm/react/react/0.14.6/package.tgz ~/.npm/react/react/0.14.6/package/ ~/.npm/react/react/0.14.6/package/package.json
Empty Cache command
rm -rf ~/.npm/*
or
npm cache clean
Installation module
In summary, the installation process is this Node module
1. Issue npm install command
2.npm URL to the registry query module compressed package
3. the download package, stored in ~ / .npm directory
4. extracting archive to the current project node_modules
Note that after a module is installed, in fact, save a local duplicate. A compressed package ~ / directory under .npm. Another is the code after extracting under node_modules directory. But time is running npm install will only check node_modules directory, but does not check ~ / .npm directory. That is, if a module in the ~ / .npm compression package, but not installed in node_modules directory, npm will still download a new archive from a remote repository.
This behavior can certainly guaranteed to always get the latest code, but sometimes is not what we want. The biggest problem is that it will greatly affect the speed of installation. Even if a module archive in the cache directory, they can go to download a remote repository, how could not slow it?
In addition, on some occasions there is no network, but you want to install the module, it is clearly in the cache directory, then also not be installed.
--cache-min parameter
To solve these problems, it provides a NPM --cache-min parameter for the cache directory from the installation module.
--cache-min parameter specifies a time (in minutes), over this time only module will be downloaded from the registry.
npm install --cache-min 999999 <package-name>
The above command specifies that only more than 999,999 minutes of module was downloaded from the registry. Actually specified, all modules are installed from the cache, thus greatly accelerate the download speed.
It also has another way
npm install --cache-min Infinity <package-name>
Offline solutions installed
The first category, Registry agency
- npm-proxy-cache
- local-npm
- asl-lazy
Usage is very similar to the above three modules are in the machine from a Registry service, all npm install command to go through this service agent.
npm-proxy-cache
npm --proxy http://localhost:8080\ --https-proxy http://localhost:8080\ --strict-ssl false\ install
local-npm
above sea level, set registry http: // 127.0.0.1:5080
asl-lazy
npm --registry http://localhost:8080/ install socket.io
With Registry service this machine will be able to fully implement caching installed, it can use offline
The second category, npm install substitute
If we can change the behavior of npm install, the installation will be able to implement caching. npm-cache tool is this idea. Any use of npm install the place, you can use npm-cache substitution
npm-cache install
The third category, node_modules as the cache directory
The idea of this program is not used .npm cache, node_modules directory instead of using the project as a cache.