2016-11-23 167 views
1

我們希望創建在AWS Lambda上運行的微應用程序。我們正在爲此調查web​​pack 2。但是,我們有遺留代碼,它使用fs.readdirSync來獲取文件/模塊名稱列表以生成模塊列表。執行該包時,我們得到一個錯誤Error: ENOENT: no such file or directory, scandir '/innerLib',因爲webpack不知道在文件lib/lib.js中執行fs.readdirSync(path.resolve(__dirname, 'innerLib'));並在包時間期間解析文件名的數組。Webpack軟件包ENOENT:無此文件或目錄fs.readdirSync

如果不對遺留代碼進行重大更改,我們可以採用wepback採取哪些方法。我已經包括下面一個簡單的例子和​​上github

webpack.config.js

var path = require('path'); 
var webpack = require('webpack'); 

module.exports = { 
    context: __dirname, 
    entry: ['./index.js'], 
    output: { 
    filename: 'bundle.js', 
    }, 
    target: 'node', 
} 

index.js

const lib = require('./lib/lib.js'); 

lib.getModuleList((err, modules) => console.log(modules)); 

LIB/lib.js

const fs = require('fs'); 
const path = require('path'); 
let moduleList = []; 
let list = fs.readdirSync(path.resolve(__dirname, 'innerLib')); 

exports.getModuleList = getModuleList; 

function getModuleList(callback) { 
    return callback(null, moduleList); 
} 

list.forEach(filename => { 
    moduleList.push({ 
    name: filename 
    }); 
}); 

的lib/innerLib/a.js

console.log('a lib loaded'); 

的lib/innerLib/b.js

console.log('b lib loaded'); 
+0

你是怎麼執行這個包的? –

回答

3

您的問題是__dirname解析爲/。爲了得到它與工作的WebPack設置:

node: { 
    __dirname: true 
} 
在webpack.config.js

。添加後,你的包對我來說執行得很好。

相關問題