{"id":18519,"library":"lws-rewrite","title":"lws-rewrite","description":"lws-rewrite is a middleware plugin for lws (Local Web Server) that adds URL rewriting support. Current stable version is 4.0.0, released under the ISC license. It allows defining rewrite rules via command-line options to map source URLs to local or remote destinations, supporting pattern-based replacements. Key differentiator: integrates seamlessly with lws's plugin architecture and its wiki provides detailed usage examples. The project is maintained with regular updates, compatible with Node.js >=12.17.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lwsjs/rewrite","tags":["javascript","lws","lws-middleware","url","rewriting","rewrite","proxy"],"install":[{"cmd":"npm install lws-rewrite","lang":"bash","label":"npm"},{"cmd":"yarn add lws-rewrite","lang":"bash","label":"yarn"},{"cmd":"pnpm add lws-rewrite","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency - this plugin is designed to be used with lws","package":"lws","optional":false}],"imports":[{"note":"lws itself is ESM-only since v4, but lws-rewrite is loaded as a plugin, not directly imported.","wrong":"const lws = require('lws')","symbol":"lws","correct":"import lws from 'lws'"},{"note":"The plugin is exported as a default class; CommonJS require is not supported as the package is ESM-only.","wrong":"const Rewrite = require('lws-rewrite')","symbol":"Rewrite","correct":"import Rewrite from 'lws-rewrite'"},{"note":"Rewrite rules are passed via CLI to lws, not used programmatically in most cases.","wrong":"node server.js --rewrite ...","symbol":"lws command line","correct":"npx lws --rewrite '/old -> /new'"}],"quickstart":{"code":"import lws from 'lws'\nimport Rewrite from 'lws-rewrite'\n\nconst server = lws.create({\n  port: 8080,\n  stack: [new Rewrite()],\n  rewrite: [\n    '/api/* -> http://localhost:3000/api/$1',\n    '/old-path -> /new-path'\n  ]\n})\n\nserver.start().then(() => {\n  console.log('Server started on http://localhost:8080')\n})","lang":"typescript","description":"Demonstrates programmatic usage of lws-rewrite plugin with lws, setting up rewrite rules to proxy API requests and redirect a path."},"warnings":[{"fix":"Use format '/from -> /to' with spaces around '->'.","message":"Rewrite rules must use '->' with spaces; missing spaces will cause the rule to be ignored.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Upgrade to Node.js >=12.17 and use ESM imports.","message":"Version 4.0.0 dropped support for Node.js <12.17 and switched to ESM-only.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Use '--rewrite' instead of deprecated options.","message":"The '--rewrite' option replaces older '--map' or custom middleware; use '--rewrite' for current API.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Order rules from most specific to least specific.","message":"Rules are applied in order; first matching rule wins, which may lead to unexpected behavior if overlapping patterns.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use lws proxy middleware for full proxy support.","message":"Rewriting to remote destinations does not alter the Host header; remote server may reject requests.","severity":"gotcha","affected_versions":"<4.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 lws-rewrite' in your project.","cause":"Package not installed or not in node_modules.","error":"Error: Cannot find module 'lws-rewrite'"},{"fix":"Use ESM import: import Rewrite from 'lws-rewrite'","cause":"Using CommonJS require() with an ESM-only package.","error":"TypeError: lws_rewrite_1.default is not a constructor"},{"fix":"Use correct format: '/old -> /new'","cause":"Rule syntax missing required spaces around '->'.","error":"lws: rewrite rule '/old-> /new' is invalid. Must contain ' -> ' (space-dash-greaterthan-space)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}