E.g. if I have an “error.ts” file that just contains:
throw new Error('error');
Then, “fn.ts” imports that file:
import './error';
export default function fn() {}
If I want to dynamically import “fn.ts” from a variable containing the file name, I’d need to use require()
or import()
. If I import “fn.ts” using require()
, I get Error: error
as expected:
const { default: fn } = require(filename); // this line throws
fn();
If I import “fn.ts” using import()
, I get:
Cannot read properties of undefined (reading 'default')
.
const { default: fn } = await import(filename); // no error
fn(); // this line throws
What are some possible explanations for why this is occurring? I’m using the latest Webpack 5.97.1. My Webpack config is too complicated to share here, but there’s no weird custom plugins etc affecting import()
.