Skip to content

ESM build contains module.exports, causing handler export overwrite in bundled environments #28

@hideokamoto

Description

@hideokamoto

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

  1. Add class-resolver v4.0.0 as a dependency
  2. Bundle with AWS CDK NodejsFunction (uses esbuild)
  3. Deploy and invoke the Lambda function
  4. 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:

  1. Check the output format in vite.config.ts for the ESM build
  2. Adjust settings to prevent CJS compatibility code from mixing into the ESM build
  3. 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

  1. Confirm dist/index.mjs does not contain module.exports
  2. Confirm dist/index.cjs contains module.exports = i;
  3. Verify imports/exports work correctly in both ESM and CJS
  4. Verify bundling and execution in AWS Lambda works correctly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions