![](/img/trans.png)
[英]Why can't my Titanium build app find a module even though it's clearly there?
[英]Why can't a peerDependency be imported or required, even when it's present in the parent module?
我正在尝试使用 npm peerDependencies
但似乎没有像宣传的那样有效。 我错过了什么?
设置是,我有两个模块, mod
和plugin
,这两个模块都依赖于 npm 的外部模块。 mod
声明了对plugin
和外部模块的硬依赖,插件声明了对等依赖,以便访问父模块使用的版本。
文件如下所示:
~/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')
正如我从文档和示例中理解的那样,我认为这应该是使用对等依赖项的标准方法,并且在任一目录中运行npm install
都不会给出任何错误或警告。
但是,当我在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
这里出了什么问题,webpack 不应该使用父mod
模块的对等依赖来解析plugin
内部的 require 语句吗?
Gah,看来这是一个边缘情况,仅影响通过文件系统在本地相互引用的模块。
解决方案显然是添加以下内容:
resolve: {
alias: {
'pad-right': path.resolve('node_modules', 'pad-right'),
},
},
到您的webpack配置。 (或者尝试resolve.symlinks: false
,这可以解决我发布的最少repro代码中的问题,但不能解决实际项目中的问题)。
对等依赖项不会按照以下链接中的说明自动安装:
https://blog.npmjs.org/post/110924823920/npm-weekly-5
更多信息可以在这里找到:
https://nodejs.org/en/blog/npm/peer-dependencies/#the-solution-peer-dependencies
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.