简体   繁体   English

如何获得另一个依赖的依赖?

[英]How to get a dependency of another dependency?

I have a package ( babel-preset-es2015-webpack ) with dependency babel-preset-es2015 . 我有一个包( babel-preset-es2015-webpack )与依赖babel-preset-es2015 babel-preset-es2015 have dependency babel-plugin-transform-es2015-modules-commonjs . babel-preset-es2015有依赖性babel-plugin-transform-es2015-modules-commonjs

How do I require babel-plugin-transform-es2015-modules-commonjs in a way to make sure that it is the same package that my babel-present-es2015 dependency is using? 我如何要求babel-plugin-transform-es2015-modules-commonjs以确保它与我的babel-present-es2015依赖关系使用的包相同?

Simply doing: 简单地做:

require('babel-plugin-transform-es2015-modules-commonjs');

Will work in NPM3 setup (assuming there are no other dependencies that require a different version of babel-plugin-transform-es2015-modules-commonjs ), but will not work in NPM2. 将在NPM3设置中工作(假设没有其他依赖项需要不同版本的babel-plugin-transform-es2015-modules-commonjs ),但在NPM2中不起作用。

If you do not want to use shrinkwrap as noted by @Nocturno, you can also simply require the specific path to the dependency: 如果你不希望使用shrinkwrap由@Nocturno注意,你也可以简单地要求对依赖特定的路径:

require('<path-to-node_modules>/babel-preset-es2015-webpack/node_modules/babel-plugin-transform-es2015-modules-commonjs');

Never used the plugins myself, but something like the above should work. 从来没有使用过插件,但上面的内容应该可行。

Another option is to lock down babel-preset-es2015-webpack to a specific version, then determine what version of babel-plugin-transform-es2015-modules-commonjs is listed in it's package.json and add it to your package.json. 另一个选择是将babel-preset-es2015-webpack到特定版本,然后确定它的package.json中列出了哪个版本的babel-plugin-transform-es2015-modules-commonjs并将其添加到package.json中。 Then you can use the require('babel-plugin-transform-es2015-modules-commonjs'); 然后你可以使用require('babel-plugin-transform-es2015-modules-commonjs'); method in NPM2 and NPM3. NPM2和NPM3中的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM