I have written a library that exports a single object like this
export default class JWTSigner
This works fine in the tests for the library where it is imported like this:
import JWTSigner from '../index';
When consuming the package I import it like this:
import JWTSigner from 'jwt-on-kms';
which does not result in any Typescript errors. When I run esbuild and run the resulting bundle I get this error
{"errorType":"TypeError","errorMessage":"import_jwt_on_kms.default is not a constructor","stack":["TypeError: import_jwt_on_kms.default is not a constructor"," at Object.<anonymous> (/var/src/functions/register.ts:71:43)"," at Module._compile (node:internal/modules/cjs/loader:1105:14)"," at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)"," at Module.load (node:internal/modules/cjs/loader:981:32)"," at Function.Module._load (node:internal/modules/cjs/loader:822:12)"," at Module.require (node:internal/modules/cjs/loader:1005:19)"," at require (node:internal/modules/cjs/helpers:102:18)"," at _tryRequireFile (file:///var/runtime/index.mjs:869:37)"," at _tryRequire (file:///var/runtime/index.mjs:919:25)"," at _loadUserApp (file:///var/runtime/index.mjs:945:22)"]}
And if I log the JWTSinger object at this point I get: { default: [class JWTSigner2] }. If I modify the bundle to use JWTSigner.default.default as the constructor instead it works perfectly. My Question is why is the default export getting wrapped in another object and how can I prevent this?
My build script is:
esbuild.build({
entryPoints: entryPoints,
bundle: true,
outdir: path.join(__dirname, outdir),
outbase: path.join(__dirname, functionDir),
platform: 'node',
external: ['aws-sdk'],
minify: isProd,
sourcemap: !isProd,
}).catch(() => process.exit(1))