2016-06-07 89 views
2

我的webpack項目依賴於一個依賴「蘋果」(組成名稱)。 「蘋果」依賴依靠NODE_PATH設置爲NODE_PATH=/path/to/apple-module。如果您不熟悉這種方法,請閱讀How I Work Around The require(「../../../../../../../」) Problem In NodeJS(我不贊同這種方法。)如何爲特定模塊配置模塊回退路徑?

我對「apple」模塊沒有任何控制權。

webpack允許配置fallback路徑,當設置爲/path/to/apple-module時使得「apple」模塊的分辨率工作。但是,這種方法會使整個應用程序模塊解析邏輯處於危險之中。

有沒有辦法設置僅適用於特定模塊的回退路徑?

下面是使用該圖案的公共模塊的一個例子:

https://github.com/firstopinion/formatter.js/blob/51c068bed4e78ba5db7f44911f7ae8ef259f692f/dist/common/formatter.js#L8-L10

var patternMatcher = require('pattern-matcher'); 
var inptSel = require('inpt-sel'); 
var utils = require('utils'); 

注意, 「圖案匹配」, 「INPT-SEL」 和 「utils的」 是包的not dependencies 。這些文件包含在./common path中。其結果是,我得到一個錯誤:

ERROR in ./~/formatter.js/dist/common/formatter.js 
Module not found: Error: Can't resolve 'pattern-matcher' in 'c:\Dev\repositories\krypton\kwak-client\node_modules\formatter.js\dist\common' 
@ ./~/formatter.js/dist/common/formatter.js 11:21-47 

ERROR in ./~/formatter.js/dist/common/formatter.js 
Module not found: Error: Can't resolve 'inpt-sel' in 'c:\Dev\repositories\krypton\kwak-client\node_modules\formatter.js\dist\common' 
@ ./~/formatter.js/dist/common/formatter.js 12:14-33 

ERROR in ./~/formatter.js/dist/common/formatter.js 
Module not found: Error: Can't resolve 'utils' in 'c:\Dev\repositories\krypton\kwak-client\node_modules\formatter.js\dist\common' 
@ ./~/formatter.js/dist/common/formatter.js 13:12-28 

回答

-1

不知道我得到它的權利,但似乎你需要依靠的WebPack別名,看https://webpack.github.io/docs/resolving.html#aliasing

+0

這僅僅是用於設置模塊的別名在應用程序本身內。問題是模塊試圖'require('conf/example.json')',它希望解析爲'./ node_modules/apple/conf/example.json',如果設置了'NODE_PATH' 。但是這個設置在webpack安裝程序中不起作用。因此,這個問題。 – Gajus