fix: load CJS appenders for ESM projects

This commit is contained in:
Lam Wei Li 2022-07-04 00:25:30 +08:00
parent 3582a00747
commit 60c9e53da0
No known key found for this signature in database
GPG Key ID: 90F6ABECF080D7BF

View File

@ -22,10 +22,18 @@ coreAppenders.set('tcp', require('./tcp'));
const appenders = new Map();
const tryLoading = (modulePath, config) => {
debug('Loading module from ', modulePath);
let resolvedPath;
try {
const modulePathCJS = `${modulePath}.cjs`;
resolvedPath = require.resolve(modulePathCJS);
debug('Loading module from ', modulePathCJS);
} catch (e) {
resolvedPath = modulePath;
debug('Loading module from ', modulePath);
}
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
return require(modulePath);
return require(resolvedPath);
} catch (e) {
// if the module was found, and we still got an error, then raise it
configuration.throwExceptionIf(