Registry / web-framework / nextjs-middleware-chain

nextjs-middleware-chain

JSON →
library0.0.8jsnpmunverified

A lightweight, zero-dependency Next.js middleware library for chaining middleware functions across API routes and SSR (getServerSideProps) data fetching. Version 0.0.8 (early-stage). Allows reuse of middleware functions by composing them in a chainable API. Differentiators: unified middleware interface for both API and SSR routes, chainable methods, global and per-route options. Release cadence: infrequent, no stable release yet.

npm install nextjs-middleware-chain
INSTALL
IMPORT
SIG · NEXTJS-MIDDLEWARE-
N
nextjs-middleware-chain
web-frameworkjavascriptv0.0.8
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

createMiddleware
✓ import { createMiddleware } from 'nextjs-middleware-chain'
✗ const createMiddleware = require('nextjs-middleware-chain')
ESM-only package; CommonJS require will not work.
default export
✓ import mwFactory from './middleware'
✗ import { default as mwFactory } from './middleware'
The package exports only 'createMiddleware' as a named export, NOT a default export. The example in README shows importing a default from a local file, not from the package.
finished route
✓ mwFactory().mwFunction().finish(routeFunction, 'Route Name')
✗ mwFactory().finish(routeFunction)
Chaining requires calling the middleware functions before finish(). Finish() expects a route handler and an optional name.

Shows setup with createMiddleware, then chaining logger and finish in an API route.

// middleware.js import { createMiddleware } from 'nextjs-middleware-chain'; const logger = (req, res, next) => { console.log('Request:', req.method, req.url); return next(); }; const mwFactory = createMiddleware([logger], { useChainOrder: true }); export default mwFactory; // pages/api/hello.js import mwFactory from '../../middleware'; const handler = (req, res) => { res.status(200).json({ message: 'Hello' }); }; export default mwFactory().logger().finish(handler, 'Hello API');
Debug
Known issues
gotchaMiddleware functions must call next() and return its result, or the chain breaks.
fix
Always return next() at the end of each middleware function.
affects: >=0.0.0
breakingPackage is in early stage (v0.0.8); API may change without major semver bump.
fix
Pin exact version and test upgrades carefully.
affects: <=0.0.8
gotchaThe 'finish' method is required at the end of the chain; otherwise the route handler is never called.
fix
Always chain .finish(handler, name) after middleware functions.
affects: >=0.0.0
gotchaOptions passed to createMiddleware or the factory are not deeply merged; they override defaults entirely.
fix
Pass full options object each time; do not rely on partial overrides.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: middlewareFunctionsArray is not iterable
createMiddleware expects an array but a single function was passed.
fix
Wrap the function in an array: createMiddleware([myFn])
TypeError: Cannot read properties of undefined (reading 'finish')
Middleware factory returned undefined because no chainable method was called before finish.
fix
Ensure chaining syntax: mwFactory().middlewareFn().finish(handler)
Error: middleware function must return a promise or call next
Middleware function did not call next() or return next().
fix
Add 'return next();' at the end of each middleware function.
Upgrade
Version history
0.0.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
nextjs-middleware-chain — npm install nextjs-middleware-chain · libregistry