From a3fad0351f4c2c3d20b1edeaa339969f9e0a56ea Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Mon, 4 Jul 2022 00:25:30 +0800 Subject: [PATCH] fix: load CJS appenders for ESM packages --- lib/appenders/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/appenders/index.js b/lib/appenders/index.js index 3e26c127..2b4b47e1 100644 --- a/lib/appenders/index.js +++ b/lib/appenders/index.js @@ -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(