Middy is a Node.js middleware engine specifically designed for AWS Lambda functions, bringing an Express.js-like middleware pattern to serverless environments. It allows developers to encapsulate common logic such as input parsing, validation, authentication, and error handling into reusable middleware functions, enhancing code modularity and maintainability for Lambda handlers. The current stable version series is 7.x, with recent releases like 7.3.1 focusing on improvements, security fixes, and compatibility. Previously, there was a significant breaking change from the 0.x series to 1.x, and subsequently to the 7.x series, requiring migration for older projects. Middy provides a rich ecosystem of official and third-party middlewares for common Lambda patterns, differentiating itself by its focus on simplicity, performance, and a clear execution lifecycle for `before`, `after`, and `onError` hooks.
npm install middyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up an AWS Lambda handler with Middy.js, applying `jsonBodyParser` for automatic JSON parsing, `validator` for schema validation, and `httpErrorHandler` for structured error responses. It showcases the ESM import style for Middy 7.x and basic middleware chaining.
Migrate to Middy 7.x using the official upgrade guides. Update all `@middy/*` packages to their latest versions and adjust import paths and middleware configurations accordingly. For example, `require('middy')` changes to `import middy from '@middy/core'` and individual middlewares are imported from their own scoped packages (e.g., `@middy/http-json-body-parser`).Upgrade your Lambda runtime to Node.js 24.x or newer. Replace `middy({ streamifyResponse: true })` with `middy({ executionMode: executionModeStreamifyResponse })` and ensure `executionModeStreamifyResponse` is imported from `@middy/core/StreamifyResponse`.Upgrade all `@middy/*` packages to at least 7.2.2 to incorporate the security fix. Regularly update dependencies to mitigate known vulnerabilities.
Upgrade `@middy/http-cors` to version 7.2.2 or newer to resolve the Punycode normalization issue.
Check for known compatibility issues between your Middy and Lambda Power Tools versions. Refer to the Middy.js and Power Tools GitHub repositories for specific fixes or workarounds. Often, upgrading both libraries to their latest compatible versions resolves such conflicts.
Upgrade `@middy/cloudformation-response` to 7.1.8 or newer. Version 7.1.8 includes a fix to auto-populate `PhysicalResourceId` from `context.logStreamName` when not explicitly provided.
Configure your bundler (e.g., esbuild) to treat `@aws/durable-execution-sdk-js` as an external package. For esbuild, add `--external:@aws/durable-execution-sdk-js` to your build command or configure it in your `esbuild.config.js`.
Update your `jest.config.js` to properly transform `@middy/core` for ESM. This often involves `transformIgnorePatterns` (e.g., `/node_modules/(?!(@middy/core)/)`), `moduleNameMapper` (`'^@middy/core$': '<rootDir>/node_modules/@middy/core'`), and configuring `ts-jest` to `useESM: true`, along with a `babel.config.js`.
Upgrade your `@middy/http-cors` package to version 7.2.2 or higher to receive the fix for Punycode normalization.
Upgrade `@middy/http-response-serializer` to version 7.1.5 or newer to resolve the type error.
Upgrade `@middy/http-event-normalizer` to the latest 7.x patch version (e.g., 7.1.5 or newer) that includes support and fixes for API Gateway Event v2.