Merge pull request #1280 from log4js-node/1277-when-the-type-is-module-there-is-an-error-when-reading-appenders-how-to-deal-with-it

fix: load CJS appenders for ESM packages
This commit is contained in:
Lam Wei Li 2022-07-04 01:32:35 +08:00 committed by GitHub
commit d2557dad3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

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(