When trying to access a module I created util.js
inside of a file where I use module.exports
in webpack, it results in an empty object at build time. In the image below, my const utils = require(...)
returns an empty object.
However, when I inspect my const utils = require(...)
inside of my main script background-script.js
, which doesn't use a module.exports, it's defined.
Problem
Webpack doesn't seem to be resolving the require
's inside the modules I'm using i'm exporting.
Project File Structure
app
├──plugins
│ ├── index.js (exports all my plugins)
| ├──plugin1
| │ ├── index.js (simply exports an object)
| |
| |──plugin2
| ├── index.js (simply exports an object)
|
|--utils.js
|--background-script.js
app/background-script.js
const utils = require('../../util.js');
app/utils.js
const utils ={....};
utils.plugins = require('./plugins/index.js');
module.exports = utils;
plugins/index.js
//require all `index.js` file from each plugin directory; works great.
const context = require.context('.', true, /index\.js/);
const requireAllPlugins = function(ctx) {
const keys = ctx.keys();
const values = keys.map(ctx);
return values;
}
const allPlugins = requireAllPlugins(context);
module.exports = [...allPlugins];
plugin1/index.js
//utils is an empty object
const utils = require('../../util.js');
module.exports = {action: utils.renderBookmark}