Webpack软件包ENOENT:没有这样的文件或目录fs.readdirSync
我们希望创build在AWS Lambda上运行的微型应用程序。 我们正在为此调查webpack 2。 但是,我们使用fs.readdirSync
遗留代码来获取文件/模块名称的列表以生成模块列表。 当执行包时,我们得到一个错误Error: ENOENT: no such file or directory, scandir '/innerLib'
因为webpack不知道执行fs.readdirSync(path.resolve(__dirname, 'innerLib'));
在文件lib/lib.js
并在捆绑时间内parsing文件名的数组。
我们可以采用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');
你的问题是__dirname
parsing为/
。 要使它与webpack一起工作,请设置:
node: { __dirname: true }
在你的webpack.config.js中。 添加后,你的捆绑包对我来说很好。