{"id":18784,"library":"serverless-middleware","title":"serverless-middleware","description":"Serverless Framework plugin (v3.4.0) enabling middleware chaining directly in serverless.yaml for AWS Lambda functions. Allows defining pre/post handlers, error catches, and execution termination via context.end(). Supports Node.js 14+ (12x–22x) and ES6 modules since v3.2.0. Differentiator: no code changes required—all wiring in YAML; supports mixed then/catch chains and TypeScript with webpack workaround. Released semi-annually, breaking changes limited to runtime/Serverless major bumps.","status":"active","version":"3.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/juanjoDiaz/serverless-middleware","tags":["javascript","serverless","plugin","middleware"],"install":[{"cmd":"npm install serverless-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add serverless-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add serverless-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency; plugin only works with Serverless v3+","package":"serverless","optional":false}],"imports":[{"note":"Must be added in serverless.yaml plugins section. Plugin registers automatically; no code import needed.","wrong":"plugins:\n  - serverlessMiddleware","symbol":"plugin","correct":"plugins:\n  - serverless-middleware"},{"note":"Middleware handlers must return a Promise or be async. Callback-style handlers are not supported.","wrong":"module.exports.authenticate = (event, context, callback) => { ... };","symbol":"middleware.handler","correct":"// handler.js\nexport const authenticate = async (event, context) => { ... };"},{"note":"Since v3.2.0, package supports ES6 import. In TypeScript + webpack, set extensionAlias to avoid build errors.","wrong":"const serverlessMiddleware = require('serverless-middleware');","symbol":"ESM import","correct":"import serverlessMiddleware from 'serverless-middleware';"}],"quickstart":{"code":"npm install serverless-middleware --save-dev\n# serverless.yaml\nplugins:\n  - serverless-middleware\nprovider:\n  name: aws\n  runtime: nodejs22.x\nfunctions:\n  myFunction:\n    handler: handler.main\n    middleware:\n      - auth.authenticate\n      - then: handler.main\n      - catch: handler.error\n\n# handler.js\nexport const authenticate = async (event, context) => {\n  if (!event.headers.Authorization) {\n    throw new Error('Unauthorized');\n  }\n  return { ...event, auth: true };\n};\nexport const main = async (event) => ({\n  statusCode: 200,\n  body: JSON.stringify({ message: 'Hello' }),\n});\nexport const error = async (error, event, context) => ({\n  statusCode: 500,\n  body: JSON.stringify({ error: error.message }),\n});","lang":"javascript","description":"Install plugin, configure middleware chains in YAML, and define async handlers. Shows auth, main, and error handlers."},"warnings":[{"fix":"Migrate from 'custom.middleware' to 'functions.myFunction.middleware' array.","message":"In v3.2.0, drop custom property from function config and use middleware directly","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Upgrade to Node 14+ and Serverless v3.","message":"v1.0.0 drops Node 10 & 12 support, requires Serverless v3","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Migrate to Node 14+ runtime.","message":"Node.js 12.x runtime support deprecated in v3.1.0","severity":"deprecated","affected_versions":">=3.1.0 <3.3.0"},{"fix":"In webpack config, add: resolve: { extensionAlias: { '.js': ['.ts', '.js'] } }","message":"ESM support (v3.2.0) breaks TypeScript + webpack builds without extensionAlias","severity":"gotcha","affected_versions":">=3.2.0"},{"fix":"Convert handlers to async functions or return Promise.","message":"Middleware using callback-style (3-arg) will not execute; only async/Promise handlers work","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Check context.prev !== undefined before using it.","message":"context.prev contains previous handler's return value; empty if first handler","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure Serverless v3+ and add 'serverless-middleware' to plugins list in serverless.yaml","cause":"Serverless version < 2 or plugin not properly added to plugins array","error":"Error: The \"middleware\" property is not supported by this framework version"},{"fix":"Use relative path like 'src/auth.authenticate'; ensure file exports the function","cause":"Middleware path is not a valid file path; plugin expects relative path without extension","error":"Cannot find module 'auth.authenticate'"},{"fix":"Ensure the middleware handler is an async function or returns a Promise","cause":"Middleware function does not return a Promise or is undefined","error":"TypeError: handler is not a function or its return value is not a Promise"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}