Webpack bundle ENOENT: no such file or directory fs.readdirSync
<p>We would like to create micro apps that run on AWS Lambda. We are investigating webpack 2 for this. However, we have legacy code that uses <code>fs.readdirSync</code> to get a list of file/module names to generate a module list. When executing the bundle we get an error <code>Error: ENOENT: no such file or directory, scandir '/innerLib'</code> because webpack does not know to execute <code>fs.readdirSync(path.resolve(__dirname, 'innerLib'));</code> in file <code>lib/lib.js</code> and resolve an array of file names during bundle time. </p>

<p>What approaches can we take with wepback without making major changes to the legacy code. I have included a simple example below and on <a href="https://github.com/isurfbecause/webby" rel="nofollow noreferrer">github</a></p>

<p><strong>webpack.config.js</strong></p>

<pre><code>var path = require( 'path' );
var webpack = require( 'webpack' );

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

<p><strong>index.js</strong></p>

<pre><code>const  lib = require('./lib/lib.js');

lib.getModuleList((err, modules) =&gt; console.log(modules));
</code></pre>

<p><strong>lib/lib.js</strong></p>

<pre><code>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 =&gt; {
  moduleList.push({
    name: filename
  });
});
</code></pre>

<p><strong>lib/innerLib/a.js</strong></p>

<pre><code>console.log('a lib loaded');
</code></pre>

<p><strong>lib/innerLib/b.js</strong></p>

<pre><code>console.log('b lib loaded');
</code></pre>