简体   繁体   English

为什么不能导入或需要 peerDependency,即使它存在于父模块中?

[英]Why can't a peerDependency be imported or required, even when it's present in the parent module?

I'm trying to use npm peerDependencies but nothing seems to work as advertised.我正在尝试使用 npm peerDependencies但似乎没有像宣传的那样有效。 What am I missing?我错过了什么?

The setup is, I have two modules, mod and plugin , both of which depend on an external module from npm.设置是,我有两个模块, modplugin ,这两个模块都依赖于 npm 的外部模块。 mod declares a hard dependency on both plugin and the external module, and the plugin declares a peer dependency, in order to get access to the version being used by the parent module. mod声明了对plugin和外部模块的硬依赖,插件声明了对等依赖,以便访问父模块使用的版本。

The files look like this:文件如下所示:

~/plugin/package.json:

    {
      "name": "plugin",
      "version": "1.0.0",
      "main": "index.js",
      "peerDependencies": {
        "pad-right": "^0.2.2"
      }
    }

~/plugin/index.js

    var peerDependency = require('pad-right')
    module.exports = 'this is a plugin'

~/mod/package.json:

    {
      "name": "mod",
      "version": "1.0.0",
      "main": "index.js",
      "dependencies": {
        "pad-right": "^0.2.2",
        "plugin": "../plugin"
      }
    }

~/mod/index.js:

    var hardDependency = require('pad-right')
    var plugin = require('plugin')

As I understand it from docs and examples, I think this should be a standard way to use peer dependencies, and running npm install in either directory doesn't give any errors or warnings.正如我从文档和示例中理解的那样,我认为这应该是使用对等依赖项的标准方法,并且在任一目录中运行npm install都不会给出任何错误或警告。

However when I run webpack in the mod folder, I get errors like:但是,当我在mod文件夹中运行 webpack 时,出现如下错误:

ERROR in ../plugin/index.js
Module not found: Error: Can't resolve 'pad-right' in '~/plugin'
 @ ../plugin/index.js 1:21-41
 @ ./index.js

What's going wrong here, shouldn't webpack resolve the require statement inside plugin with the peer dependency from the parent mod module?这里出了什么问题,webpack 不应该使用父mod模块的对等依赖来解析plugin内部的 require 语句吗?

Gah, it looks like this is an edge case that only affects modules that are referencing each other locally via the file system. Gah,看来这是一个边缘情况,仅影响通过文件系统在本地相互引用的模块。

The solution is apparently to add something like: 解决方案显然是添加以下内容:

    resolve: {
        alias: {
            'pad-right': path.resolve('node_modules', 'pad-right'),
        },
    },

to your webpack config. 到您的webpack配置。 (Or else to try resolve.symlinks: false , which solves the problem in the minimal repro code I posted, but doesn't solve things in my actual project). (或者尝试resolve.symlinks: false ,这可以解决我发布的最少repro代码中的问题,但不能解决实际项目中的问题)。

Article about the issue 关于这个问题的文章

Peer dependencies are not automatically installed as explained in the following link: 对等依赖项不会按照以下链接中的说明自动安装:

https://blog.npmjs.org/post/110924823920/npm-weekly-5 https://blog.npmjs.org/post/110924823920/npm-weekly-5

More information can be found here: 更多信息可以在这里找到:

https://nodejs.org/en/blog/npm/peer-dependencies/#the-solution-peer-dependencies https://nodejs.org/en/blog/npm/peer-dependencies/#the-solution-peer-dependencies

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

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