The ESM build (dist/index.mjs) of class-resolver v4.0.0 includes module.exports = i;. When inlined by bundlers (esbuild, webpack, etc.), this overwrites the module.exports of Lambda handlers and other entry points, causing runtime errors.
Reproduction Steps
- Add
class-resolver v4.0.0 as a dependency
- Bundle with AWS CDK
NodejsFunction (uses esbuild)
- Deploy and invoke the Lambda function
- Error:
Runtime.HandlerNotFound: index.main is undefined or not exported
Root Cause
Line 159 of dist/index.mjs contains:
module.exports = i;
export {
i as default
};
The ESM module includes module.exports, which can overwrite the caller's module.exports when inlined by bundlers.
Affected Environments
- AWS Lambda (NodejsFunction/esbuild)
- Other environments using esbuild/webpack for bundling
- Direct ESM usage is unaffected (Node.js ESM ignores
module.exports)
Expected Behavior
- ESM build (
dist/index.mjs): Only export default i;
- CJS build (
dist/index.cjs): Only module.exports = i;
Proposed Fix
Adjust the Vite build configuration to exclude module.exports from the ESM build.
Recommended Fix:
- Check the output format in
vite.config.ts for the ESM build
- Adjust settings to prevent CJS compatibility code from mixing into the ESM build
- Alternatively, separate ESM/CJS exports in the source code
Points to Check:
- Vite
build.lib.formats configuration
- Settings for
rollup-plugin-dts or vite-plugin-dts
- Ensure ESM/CJS exports are not mixed in the source code
Temporary Workaround (Consumer Side)
Consumers can use the following workaround (not a permanent fix):
// Force CJS build resolution using require()
const Resolver = require("class-resolver");
Or prioritize CJS in bundler configuration:
bundling: {
mainFields: ['main', 'module'], // Prioritize CJS
}
Verification After Fix
- Confirm
dist/index.mjs does not contain module.exports
- Confirm
dist/index.cjs contains module.exports = i;
- Verify imports/exports work correctly in both ESM and CJS
- Verify bundling and execution in AWS Lambda works correctly
The ESM build (
dist/index.mjs) ofclass-resolver v4.0.0includesmodule.exports = i;. When inlined by bundlers (esbuild, webpack, etc.), this overwrites themodule.exportsof Lambda handlers and other entry points, causing runtime errors.Reproduction Steps
class-resolver v4.0.0as a dependencyNodejsFunction(uses esbuild)Runtime.HandlerNotFound: index.main is undefined or not exportedRoot Cause
Line 159 of
dist/index.mjscontains:The ESM module includes
module.exports, which can overwrite the caller'smodule.exportswhen inlined by bundlers.Affected Environments
module.exports)Expected Behavior
dist/index.mjs): Onlyexport default i;dist/index.cjs): Onlymodule.exports = i;Proposed Fix
Adjust the Vite build configuration to exclude
module.exportsfrom the ESM build.Recommended Fix:
vite.config.tsfor the ESM buildPoints to Check:
build.lib.formatsconfigurationrollup-plugin-dtsorvite-plugin-dtsTemporary Workaround (Consumer Side)
Consumers can use the following workaround (not a permanent fix):
Or prioritize CJS in bundler configuration:
Verification After Fix
dist/index.mjsdoes not containmodule.exportsdist/index.cjscontainsmodule.exports = i;