什么版本的npm库*实际上*使用?
我知道npm库在安装时可以在分层树中安装同一个库的多个版本,如下所示:
a@0.1.0 -> b@1.0 -> c@2.0 -> b@2.0
在上面,在版本0.1.0
软件包a
被拉入,其依赖关系为b@1.0
和c@2.0
。 类似地, c@2.0
的依赖被拉入,即b@2.0
。
我从别人那里听说,即使软件包b
安装在两个不同的版本上,其中只有一个实际上被加载到内存中并被使用。 我也听说,这可能或不可能是JavaScript的浏览器部署节点部署的情况。
所以我的问题是:只有一个b
包被加载到内存中是真的吗? 如果是这样,节点和浏览器都是这样,还是有区别?
的NodeJS
可以加载多个版本的Node模块/库,具体取决于从哪里加载。 在Node.js文档中深入介绍了模块加载器 。
require()
调用根据已parsing的文件pathcaching模块 。
require('b')
从模块a
将parsing为.../a/node_modules/b
require('b')
从模块c
将parsing为.../a/node_modules/c/node_modules/b
所以单独的模块将被加载相同的电话。 这可以用一个小例子来演示。
模块B – node_modules/b/index.js
module.exports = { VERSION = 'b-0.5.0' }
模块C – node_modules/c/index.js
module.exports = { VERSION: 'c-1.0.0', BVERSION: require('b').VERSION, }
模块C的副本B – node_modules/c/node_modules/b/index.js
module.exports = { VERSION: 'b-9.8.7', }
创build一个程序来输出版本。
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
所以两个不同path的模块使用require('b').VERSION
获得不同的值。
穿越
值得注意的是,如果Node.js require('b')
找不到本地安装的./node_modules/b
那么它将遍历目录树以在父节点node_modules
目录中查找b
。
再次使用上面的示例,但删除a/node_modules/c/node_modules/b
。
模块c
做一个require('b')
但找不到...a/node_modules/c/node_modules/b
那么它将进入父节点node_modules/
并寻找b
,在这种情况下,它会加载caching的副本of ...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包一起工作。
你应该能够用你的装载器打包上面的例子,看看它的行为。