What version of npm library is * actually used?

I know that npm libraries during installation can install several versions of the same library in a hierarchical tree, for example:

a@0.1.0
  -> b@1.0
  -> c@2.0
       -> b@2.0

In the above example, the package is unloaded ain the version 0.1.0and its dependencies b@1.0and c@2.0. Similarly, the dependence is c@2.0 drawn in, which is b@2.0.

I heard from someone that although the package is binstalled in two different versions, only one of them is actually loaded into memory and used. I also heard that this may or may not be the case with node deployments of javascript deployment and browsers.

So my question is: is it true that only one package is loaded into memory b? If so, is this true for both node and browser, or are there differences?

+4
source share
1 answer

Nodejs

Several Node module / library modules can be downloaded, depending on where they are loaded. the module loader is described in detail in the Node.js documentation .

require()the call caches the modules based on the allowed file path.

require('b')from the module will aallow.../a/node_modules/b

require('b')from the module will callow.../a/node_modules/c/node_modules/b

Therefore, separate modules will be loaded for the same call. This can be demonstrated with a small example.

Module B - node_modules/b/index.js

module.exports = {
  VERSION = 'b-0.5.0'
}

Module C - node_modules/c/index.js

module.exports = {
  VERSION: 'c-1.0.0',
  BVERSION: require('b').VERSION,
}

Module C Copy B - node_modules/c/node_modules/b/index.js

module.exports = {
  VERSION: 'b-9.8.7',
}

.

console.log('b', require('b').VERSION)
console.log('c', require('c').VERSION)
console.log('cb', require('c').BVERSION)

node index.js 
b b-0.5.0
c c-1.0.0
cb b-9.8.7

, require('b').VERSION .

Traversal

, Node.js require('b') ./node_modules/b, , b node_modules.

, a/node_modules/c/node_modules/b.

c require('b'), ...a/node_modules/c/node_modules/b, node_modules/ b. ...a/node_modules/b

node index.js 
b b-0.5.0
c c-1.0.0
cb b-0.5.0

npm. , CommonJS-, Browserify Webpack. CommonJS/ Node.js, npm.

, .

+3

Source: https://habr.com/ru/post/1683593/


All Articles