[英]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.