{"id":25980,"library":"next-middleware","title":"next-middleware","description":"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.","status":"active","version":"0.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/mrsteele/next-middleware","tags":["javascript","next","nextjs","middleware","plugin","extension"],"install":[{"cmd":"npm install next-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add next-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add next-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Designed for Next.js; middleware wraps Next.js pages and relies on Next.js request/response objects.","package":"next","optional":false},{"reason":"Peer dependency of Next.js; required for JSX in pages.","package":"react","optional":true}],"imports":[{"note":"Package is ESM-only; CommonJS require will cause error.","wrong":"const Middleware = require('next-middleware')","symbol":"Middleware","correct":"import Middleware from 'next-middleware'"},{"note":"The class is instantiated directly; no static create method.","wrong":"const middleware = Middleware.create()","symbol":"Middleware","correct":"const middleware = new Middleware()"},{"note":"The build() method returns the handler function; 'handler' is not exported.","wrong":"export default middleware.handler","symbol":"middleware.build","correct":"export default middleware.build"}],"quickstart":{"code":"import Middleware from 'next-middleware';\n\nconst middleware = new Middleware();\n\nmiddleware.add(async ({ req }) => {\n  const res = await fetch(`https://api.example.com/users?id=${req.query.id}`);\n  const users = await res.json();\n  return { users };\n});\n\nexport default middleware.build;","lang":"javascript","description":"Shows basic setup: import Middleware, create pipeline, add async middleware that fetches data, and export build method."},"warnings":[{"fix":"Ensure each middleware returns a plain object with the props to inject.","message":"Middleware functions must return an object; returned props are merged with page's getInitialProps.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Pin version in package.json and test upgrades thoroughly.","message":"The package is very early (v0.0.1); API may change without notice.","severity":"gotcha","affected_versions":"<=0.0.1"},{"fix":"Use Next.js middleware feature for App Router instead.","message":"Only works with Next.js pages directory; not compatible with App Router.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure you instantiate correctly: const middleware = new Middleware(); then add at least one middleware before exporting .build.","cause":"Attempted to call middleware.build before adding any middleware or after invalid instantiation.","error":"TypeError: middleware.build is not a function"},{"fix":"Switch to import statement: import Middleware from 'next-middleware';","cause":"Using CommonJS require to import the ESM-only package.","error":"Error: You're using the wrong import syntax. Use 'import Middleware from \"next-middleware\"' instead."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}