serverless-compose is a lightweight, functional middleware framework specifically designed for AWS Lambda functions, enabling the creation of composable, "onion-style" middleware stacks. Currently at version 2.4.0, its release cadence is not explicitly stated in the provided documentation, but it appears actively maintained. A key differentiator is its zero-dependency philosophy, ensuring a minimal bundle size and avoiding dependency conflicts. Unlike more opinionated frameworks, serverless-compose focuses on providing a thin `compose` function and flexible patterns like `recoveryMiddleware` and `timingLogMiddleware` without enforcing specific architectural designs, allowing developers to define their middleware stack with explicit, functional wrappers around their Lambda handlers. It aims to prevent the "fossilization" of bad middleware habits by promoting a clear, functional approach for event processing.
npm install serverless-composeVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic setup with `compose`, `timingLogMiddleware` for logging duration, and `recoveryMiddleware` for graceful error handling in an AWS Lambda handler.
Carefully consider the execution flow. Middleware like `recoveryMiddleware` that needs to catch errors from subsequent middleware or the handler should typically be placed earlier in the `compose` argument list (further to the right), allowing it to wrap and protect the functions composed after it.
Ensure your project's `package.json` specifies `"type": "module"` for ESM, or use a build step (e.g., esbuild, webpack) to transpile your code to CJS if your environment strictly requires it. For hybrid setups, consider using dynamic `import()` within CJS modules to load ESM dependencies.
Always include and configure `recoveryMiddleware` (or similar custom error handling) as one of the outermost middleware in your `compose` stack. Ensure its error handling function returns a valid API Gateway proxy response object (e.g., `{ statusCode: 500, body: JSON.stringify({ message: 'Error' }) }`).Verify `handler` paths in `serverless.yml` are correct relative to the service's `package.json`. For monorepos, ensure your build process correctly copies or symlinks shared code, or configure bundling plugins to resolve modules from the root. Explicitly setting `srcDir` might be needed for some bundlers.
Add `"type": "module"` to your `package.json`, or rename your file to `.mjs`, or ensure your build process (e.g., Babel, TypeScript compiler, bundler) transpiles ESM to CJS if your environment is CJS-only.
Replace all `require()` calls with `import` statements for `serverless-compose` and other dependencies. If you need to load CJS modules from an ESM context, use `await import('your-cjs-module')`.Ensure `recoveryMiddleware` is included in your `compose` stack and its error handling function is correctly implemented to return a structured API Gateway proxy response object (e.g., `{ statusCode: 500, body: JSON.stringify({ message: 'Error' }) }`). Place `recoveryMiddleware` towards the 'right' in your `compose` arguments to wrap all preceding middleware and your handler.No dependency data recorded yet.