{"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,"search_vec":"'12x':32 '14':31 '22x':33 'allow':18 'annual':60 'aw':15 'break':61 'bump':67 'catch':23 'chain':10,51 'chang':42,62 'code':41 'context.end':28 'defin':19 'differenti':39 'direct':11 'enabl':8 'error':22 'es6':35 'execut':25 'framework':5 'function':17 'handler':21 'javascript':68 'lambda':16 'limit':63 'major':66 'middlewar':3,9,71 'mix':49 'modul':36 'node.js':30 'plugin':6,70 'pre/post':20 'releas':57 'requir':43 'runtime/serverless':65 'semi':59 'semi-annu':58 'serverless':2,4,69 'serverless-middlewar':1 'serverless.yaml':13 'sinc':37 'support':29,48 'termin':26 'then/catch':50 'typescript':53 'v3.2.0':38 'v3.4.0':7 'via':27 'webpack':55 'wire':45 'workaround':56 'yaml':47","created_at":"2026-04-25T07:40:07.359016+00:00","updated_at":"2026-04-25T07:40:07.359016+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/juanjoDiaz/serverless-middleware","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/serverless-middleware","openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops","aws"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}