{"id":18828,"library":"string-replace-middleware","title":"string-replace-middleware","description":"Express middleware that performs stream-based string replacement in HTTP response bodies on the fly. Current stable version is 1.1.0, released under MIT license, with infrequent updates. Key differentiator: works on the response stream before sending, unlike typical string replacement that buffers entire responses. Supports configurable Content-Type filtering via regex (default: text/*, application/json, application/xml). Ships TypeScript type declarations. Requires Node >=10 and Express. Not recommended for production with large or streaming responses due to potential performance overhead.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/bfncs/string-replace-middleware","tags":["javascript","typescript"],"install":[{"cmd":"npm install string-replace-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add string-replace-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add string-replace-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; designed as Express middleware and requires the Express request/response object","package":"express","optional":false}],"imports":[{"note":"The library exports a named export, not a default export. ESM import syntax shown; CommonJS require works the same way.","wrong":"import stringReplace from 'string-replace-middleware'","symbol":"stringReplace","correct":"import { stringReplace } from 'string-replace-middleware'"},{"note":"CommonJS require must use destructuring to obtain the named export. Direct require returns the module object.","wrong":"const stringReplace = require('string-replace-middleware')","symbol":"stringReplace","correct":"const { stringReplace } = require('string-replace-middleware')"},{"note":"Alternative CommonJS pattern: assign the named export explicitly to a variable.","wrong":"const stringReplace = require('string-replace-middleware')","symbol":"stringReplace","correct":"const stringReplace = require('string-replace-middleware').stringReplace"}],"quickstart":{"code":"import express from 'express';\nimport { stringReplace } from 'string-replace-middleware';\n\nconst app = express();\n\napp.use(stringReplace({ 'foo': 'bar', 'hello': 'world' }));\n\napp.get('/', (req, res) => {\n  res.send('foo says hello');\n});\n\napp.listen(3000, () => console.log('Listening on 3000'));","lang":"typescript","description":"Sets up an Express app with string replacement middleware that replaces 'foo' with 'bar' and 'hello' with 'world' in all responses matching default content types."},"warnings":[{"fix":"Verify that your responses set an appropriate Content-Type header. Use the options.contentTypeFilterRegexp to customize matching.","message":"Middleware only processes responses with Content-Type matching the regex. Responses without Content-Type or with non-matching types (e.g., image/png) are passed through unmodified.","severity":"gotcha","affected_versions":"all"},{"fix":"Avoid using this middleware for large file downloads or streaming endpoints. Consider alternative approaches like pre-processing strings before sending.","message":"The middleware buffers the entire response body to perform replacement, which can cause high memory usage and increased latency for large responses or high-traffic endpoints.","severity":"gotcha","affected_versions":"all"},{"fix":"Use targeted replacements that are unlikely to appear in code or structure. Consider adding logic to avoid replacing inside certain contexts (not provided by the library).","message":"Replacement is applied to the raw response stream, including HTML tags and JavaScript code. Replacements may inadvertently break content syntax (e.g., replacing 'var' in JavaScript).","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `const { stringReplace } = require('string-replace-middleware')` instead of `const stringReplace = require('string-replace-middleware')`.","cause":"Incorrect import style — using default import instead of named import.","error":"TypeError: stringReplace is not a function"},{"fix":"Run `npm install string-replace-middleware` and ensure Node.js >= 10.","cause":"Package not installed or Node.js version < 10 (engines requirement).","error":"Cannot find module 'string-replace-middleware'"},{"fix":"Ensure you call `app.use(stringReplace({...}))` correctly. The library uses string replacement, not regex, so most special characters are safe. Check for proper Express setup.","cause":"If the replacement map contains a key that is a string with special regex characters (e.g., '.'), the library may throw due to improper regex escaping (note: actual library behavior — it uses plain string replace, not regex, so this error unlikely but possible if misuse). Alternatively, this error can occur if the middleware is not applied correctly.","error":"TypeError: Cannot read property 'foo' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}