middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵
https://middy.js.org
MIT License
3.73k stars 376 forks source link

Incompatibility of `@middy/core` with Node.js v20.16.0 in Serverless Framework Project #1230

Closed ChetanUpreti99 closed 2 months ago

ChetanUpreti99 commented 2 months ago

Bug Report: Incompatibility of @middy/core with Node.js v20.16.0 in Serverless Framework Project

Describe the Bug

When attempting to use @middy/core in an AWS Lambda handler with the following environment:

I encountered the following error:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/chetanupreti/Desktop/SELF_PROJECT/NODEJS/nodejs-serverless-learnings/user-service2/node_modules/@middy/core/package.json

This error occurs consistently when the Lambda function is executed, both in a local environment using serverless-offline and when deployed to AWS.

To Reproduce

Steps to reproduce the behavior:

  1. Set up a simple Serverless Framework project using Node.js with the following handler configuration:

    import middy from '@middy/core';
    
    export const Signup = middy(async function (_event, _context) {
        return {
            statusCode: 200,
            body: JSON.stringify({
                message: 'Hello world'
            })
        };
    });
  2. Run the function locally using serverless-offline or deploy the function to AWS and trigger it.

  3. Observe the following error message:

    Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /path/to/node_modules/@middy/core/package.json

A minimal reproducible example is available in this GitHub repository: GitHub Repo Link.

Expected Behavior

The Lambda function should execute successfully without errors, with proper recognition and functionality of the @middy/core middleware.

Environment

Additional Context

Suggested Actions:

  1. Review and update the exports field in the package.json file of @middy/core to ensure compatibility with both CommonJS and ES Module environments.
  2. Provide clear documentation on how to properly import and use @middy/core in various module systems, especially when using the Serverless Framework.

I can help you with more information or collaborate on testing potential fixes.

willfarrell commented 2 months ago

Thanks for you detailed report.

v5.x does not support CommonJS anymore due to the increasing complexities of maintaining it, but more importantly, ESM support within lambda is mature and gives a notable performance enhancement during cold starts.

Options: a) Recommend to the Serverless Framework to upgrade their build process to use ESM and take advantage of these performance gains. b) use Middy v4.x, it's really stable. c) wait for Node 22 and use --experimental-require-module (untested), use Middy 4.x in the mean time.