Registry / web-framework / next-middleware

next-middleware

JSON →
library0.0.1jsnpmunverified

A lightweight middleware pipeline for Next.js applications, enabling developers to add reusable middleware functions that execute before page rendering. Current stable version is 0.0.1, with no frequent release cadence. Key differentiators: simple API using a pipeline pattern, designed specifically for Next.js pages directory. It allows chaining multiple async middleware functions that can inject props into pages.

npm install next-middleware
INSTALL
IMPORT
SIG · NEXT-MIDDLEWARE
N
next-middleware
web-frameworkjavascriptv0.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

Middleware
import Middleware from 'next-middleware'
const Middleware = require('next-middleware')
Package is ESM-only; CommonJS require will cause error.
Middleware
const middleware = new Middleware()
const middleware = Middleware.create()
The class is instantiated directly; no static create method.
middleware.build
export default middleware.build
export default middleware.handler
The build() method returns the handler function; 'handler' is not exported.

Shows basic setup: import Middleware, create pipeline, add async middleware that fetches data, and export build method.

import Middleware from 'next-middleware'; const middleware = new Middleware(); middleware.add(async ({ req }) => { const res = await fetch(`https://api.example.com/users?id=${req.query.id}`); const users = await res.json(); return { users }; }); export default middleware.build;
Debug
Known issues
gotchaMiddleware functions must return an object; returned props are merged with page's getInitialProps.
fix
Ensure each middleware returns a plain object with the props to inject.
affects: >=0.0.1
gotchaThe package is very early (v0.0.1); API may change without notice.
fix
Pin version in package.json and test upgrades thoroughly.
affects: <=0.0.1
gotchaOnly works with Next.js pages directory; not compatible with App Router.
fix
Use Next.js middleware feature for App Router instead.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: middleware.build is not a function
Attempted to call middleware.build before adding any middleware or after invalid instantiation.
fix
Ensure you instantiate correctly: const middleware = new Middleware(); then add at least one middleware before exporting .build.
Error: You're using the wrong import syntax. Use 'import Middleware from "next-middleware"' instead.
Using CommonJS require to import the ESM-only package.
fix
Switch to import statement: import Middleware from 'next-middleware';
Upgrade
Version history
0.0.1latest on npm
Audit
Dependencies
nextrequiredDesigned for Next.js; middleware wraps Next.js pages and relies on Next.js request/response objects.
reactoptionalPeer dependency of Next.js; required for JSX in pages.
Agent activity
2 hits · last 30 days
node
2
Resources
next-middleware — npm install next-middleware · libregistry