Registry / devops / serverless-middleware

serverless-middleware

JSON →
library3.4.0jsnpmunverified

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.

npm install serverless-middleware
INSTALL
IMPORT
SIG · SERVERLESS-MIDDLEW
S
serverless-middleware
devopsjavascriptv3.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

plugin
plugins: - serverless-middleware
plugins: - serverlessMiddleware
Must be added in serverless.yaml plugins section. Plugin registers automatically; no code import needed.
middleware.handler
// handler.js export const authenticate = async (event, context) => { ... };
module.exports.authenticate = (event, context, callback) => { ... };
Middleware handlers must return a Promise or be async. Callback-style handlers are not supported.
ESM import
import serverlessMiddleware from 'serverless-middleware';
const serverlessMiddleware = require('serverless-middleware');
Since v3.2.0, package supports ES6 import. In TypeScript + webpack, set extensionAlias to avoid build errors.

Install plugin, configure middleware chains in YAML, and define async handlers. Shows auth, main, and error handlers.

npm install serverless-middleware --save-dev # serverless.yaml plugins: - serverless-middleware provider: name: aws runtime: nodejs22.x functions: myFunction: handler: handler.main middleware: - auth.authenticate - then: handler.main - catch: handler.error # handler.js export const authenticate = async (event, context) => { if (!event.headers.Authorization) { throw new Error('Unauthorized'); } return { ...event, auth: true }; }; export const main = async (event) => ({ statusCode: 200, body: JSON.stringify({ message: 'Hello' }), }); export const error = async (error, event, context) => ({ statusCode: 500, body: JSON.stringify({ error: error.message }), });
Debug
Known issues
breakingIn v3.2.0, drop custom property from function config and use middleware directly
fix
Migrate from 'custom.middleware' to 'functions.myFunction.middleware' array.
affects: >=2.0.0 <3.0.0
breakingv1.0.0 drops Node 10 & 12 support, requires Serverless v3
fix
Upgrade to Node 14+ and Serverless v3.
affects: >=1.0.0
deprecatedNode.js 12.x runtime support deprecated in v3.1.0
fix
Migrate to Node 14+ runtime.
affects: >=3.1.0 <3.3.0
gotchaESM support (v3.2.0) breaks TypeScript + webpack builds without extensionAlias
fix
In webpack config, add: resolve: { extensionAlias: { '.js': ['.ts', '.js'] } }
affects: >=3.2.0
gotchaMiddleware using callback-style (3-arg) will not execute; only async/Promise handlers work
fix
Convert handlers to async functions or return Promise.
affects: >=0.0.1
gotchacontext.prev contains previous handler's return value; empty if first handler
fix
Check context.prev !== undefined before using it.
affects: >=0.0.1
Errors
Common errors & fixes
Error: The "middleware" property is not supported by this framework version
Serverless version < 2 or plugin not properly added to plugins array
fix
Ensure Serverless v3+ and add 'serverless-middleware' to plugins list in serverless.yaml
Cannot find module 'auth.authenticate'
Middleware path is not a valid file path; plugin expects relative path without extension
fix
Use relative path like 'src/auth.authenticate'; ensure file exports the function
TypeError: handler is not a function or its return value is not a Promise
Middleware function does not return a Promise or is undefined
fix
Ensure the middleware handler is an async function or returns a Promise
Upgrade
Version history
3.4.0latest on npm
Audit
Dependencies
serverlessrequiredpeer dependency; plugin only works with Serverless v3+
Agent activity
15 hits · last 30 days
node
14
Resources