webpack require expression missing modules

Viewed 53

From webpack documentation, when a require expression is not known at compile time, it should generate a context module and include all the modules that could match the expression.

const handlers = {};

for (const name of Object.keys(SomeObject)) {
  try {
    handlers[name] = require(`./${name}.js`);
  } catch {} // eslint-disable-line no-empty
}

module.exports = handlers;

however, what I get is `

const handlers = {};

for (const name of Object.keys(SomeObject)) {
  try {
    handlers[name] = require(`./${name}.js`);
  } catch {} // eslint-disable-line no-empty
}

module.exports = handlers;

however, what I actually get is this error Cannot find module ./\u001a.js

but if I turn it into a dynamic import expression then it works

const handlers = {};

for (const name of Object.keys(SomeObject)) {
  try {
    import(`./${name}.js`).then(m => { handler[name] = m; });
  } catch {} // eslint-disable-line no-empty
}

module.exports = handlers;

But I would prefer not to do that since the offending code is from some node package

0 Answers
Related