{"id":18477,"library":"koa-rewrite","title":"koa-rewrite","description":"URL rewrite middleware for Koa. Current version 3.0.1 supports Koa 2. Release cadence is stable but infrequent. Key differentiators: simple API using regex or route parameters similar to Express, debug support via DEBUG environment variable. Alternative libraries like koa-route provide routing only.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/koajs/rewrite","tags":["javascript","koa","middleware","rewrite","redirect","url"],"install":[{"cmd":"npm install koa-rewrite","lang":"bash","label":"npm"},{"cmd":"yarn add koa-rewrite","lang":"bash","label":"yarn"},{"cmd":"pnpm add koa-rewrite","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default import; CommonJS require also works.","wrong":"const rewrite = require('koa-rewrite')","symbol":"rewrite","correct":"import rewrite from 'koa-rewrite'"}],"quickstart":{"code":"import Koa from 'koa';\nimport rewrite from 'koa-rewrite';\n\nconst app = new Koa();\napp.use(rewrite(/^\\/i(\\w+)/, '/items/$1'));\napp.use(ctx => {\n  ctx.body = ctx.url;\n});\napp.listen(3000);","lang":"typescript","description":"Basic Koa server with URL rewrite: /i123 becomes /items/123."},"warnings":[{"fix":"Install koa-rewrite@1 for koa@1, or upgrade to koa@2.","message":"koa-rewrite@2+ only supports koa@2; use koa-rewrite@1 for koa@1.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use additional middleware to handle method modifications.","message":"The rewrite does not change the HTTP method; only URL.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Anchor regex at start with ^, e.g., /^\\/old(.*)/.","message":"Regex pattern must match entire path from start; otherwise unexpected matches.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install koa-rewrite'.","cause":"Package not installed or wrong path.","error":"Error: Cannot find module 'koa-rewrite'"},{"fix":"Use 'import rewrite from 'koa-rewrite'' or 'const rewrite = require('koa-rewrite')'.","cause":"Incorrect import: the module exports a function but was imported as an object.","error":"TypeError: rewrite is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}