{"id":16126,"library":"mimic-response","title":"Mimic Node.js HTTP Response Stream Properties","description":"The `mimic-response` library is a utility designed to replicate properties and events from a Node.js `http.IncomingMessage` (an HTTP response stream) onto any generic Node.js stream. This is particularly useful in scenarios like proxying or transforming HTTP responses where the downstream stream needs to inherit metadata such as `statusCode`, `statusMessage`, and `headers` without copying the actual data payload. The current stable version is `4.0.0`. Historically, new major versions have been released roughly annually, typically coinciding with updated Node.js LTS requirements and ecosystem shifts like the move to pure ESM. Its key differentiator lies in its specific focus on mirroring *response stream properties* rather than content, providing a lightweight way to maintain HTTP context across stream transformations. It stands apart from related tools like `mimic-fn` (for functions) or broader stream cloning utilities by targeting the specific metadata of an HTTP response.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/mimic-response","tags":["javascript","mimic","response","stream","http","https","request","get","core"],"install":[{"cmd":"npm install mimic-response","lang":"bash","label":"npm"},{"cmd":"yarn add mimic-response","lang":"bash","label":"yarn"},{"cmd":"pnpm add mimic-response","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `mimic-response` package is pure ESM since v4.0.0. CommonJS users must either migrate their project to ESM or use dynamic import with `await import('mimic-response')`.","wrong":"const mimicResponse = require('mimic-response');","symbol":"mimicResponse","correct":"import mimicResponse from 'mimic-response';"},{"note":"`mimicResponse` is the default export of the package and should not be imported as a named export.","wrong":"import { mimicResponse } from 'mimic-response';","symbol":"mimicResponse","correct":"import mimicResponse from 'mimic-response';"},{"note":"The package includes TypeScript definitions. As `mimicResponse` is a default export, its type import should follow the default import syntax.","wrong":"import type { mimicResponse } from 'mimic-response';","symbol":"mimicResponse (type)","correct":"import type mimicResponse from 'mimic-response';"}],"quickstart":{"code":"import { PassThrough as PassThroughStream } from 'node:stream';\nimport mimicResponse from 'mimic-response';\n\nfunction getHttpResponseStream() {\n  // Simulate an http.IncomingMessage with relevant properties\n  const response = new PassThroughStream();\n  Object.defineProperty(response, 'statusCode', { value: 200, configurable: true });\n  Object.defineProperty(response, 'statusMessage', { value: 'OK', configurable: true });\n  Object.defineProperty(response, 'headers', { value: { 'content-type': 'application/json' }, configurable: true });\n  Object.defineProperty(response, 'rawHeaders', { value: ['Content-Type', 'application/json'], configurable: true });\n  Object.defineProperty(response, 'trailers', { value: {}, configurable: true });\n  Object.defineProperty(response, 'rawTrailers', { value: [], configurable: true });\n  return response;\n}\n\nconst responseStream = getHttpResponseStream();\nconst myStream = new PassThroughStream();\n\nmimicResponse(responseStream, myStream);\n\nconsole.log('Original status code:', responseStream.statusCode);\nconsole.log('Mimicked status code:', myStream.statusCode);\n\n// Demonstrating the 'destroy' caveat\nconst myStreamWithDestroy = new PassThroughStream({\n  destroy(error, callback) {\n    console.log('myStreamWithDestroy destroy called with error:', error ? error.message : 'none');\n    responseStream.destroy(error); // Ensure the source stream's destroy is also called\n    callback(error);\n  }\n});\n\nmimicResponse(responseStream, myStreamWithDestroy);\n// Simulate an error causing the stream to destroy\nmyStreamWithDestroy.destroy(new Error('Simulated stream error'));","lang":"typescript","description":"Demonstrates how to use `mimicResponse` to transfer HTTP response metadata like `statusCode` and `headers` from a source stream (simulated `IncomingMessage`) to a target `PassThroughStream`, and also illustrates the important `destroy` proxying caveat."},"warnings":[{"fix":"Migrate your project to use ES modules by adding `\"type\": \"module\"` to your `package.json` and using `import` statements, or ensure your consuming code is already an ES module. For existing CommonJS projects that cannot migrate, use `await import('mimic-response')` for dynamic loading.","message":"Version 4.0.0 of `mimic-response` is a pure ESM package. This means it can only be imported using `import` statements in ES modules, or dynamically with `await import()` in CommonJS. Direct `require()` calls are no longer supported.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Ensure your Node.js environment meets the minimum version requirement. Upgrade Node.js if necessary.","message":"Version 4.0.0 increased the minimum Node.js requirement to `^12.20.0 || ^14.13.1 || >=16.0.0`.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Ensure your Node.js environment is at least version 10.x. Upgrade Node.js if necessary, or downgrade `mimic-response` to a v2.x version if strict Node.js 8.x compatibility is required.","message":"Version 3.0.0 increased the minimum Node.js requirement to Node.js 10.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"fix":"Ensure your Node.js environment is at least version 8.x. Upgrade Node.js if necessary, or use `mimic-response` v1.x for older Node.js compatibility.","message":"Version 2.0.0 increased the minimum Node.js requirement to Node.js 8.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"When creating your target stream, provide a `destroy` method in its constructor options that explicitly calls the `destroy` method of the `from` stream, as shown in the package's API documentation example.","message":"The `destroy(error)` function of the source stream (`from`) is not automatically proxied to the target stream (`to`). If you need to propagate destroy calls, you must manually implement the `destroy` method on your target stream and call `from.destroy(error)` within it.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Refactor your consuming code to use `import` syntax within an ES module (by adding `\"type\": \"module\"` to `package.json` or using `.mjs` files), or use dynamic `import` like `const mimicResponse = (await import('mimic-response')).default;`.","cause":"Attempting to use `require()` to import `mimic-response` version 4.0.0 or higher in a CommonJS module.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/mimic-response/index.js from /your/project/index.js not supported."},{"fix":"Add `\"type\": \"module\"` to your `package.json` file, or rename your file to have a `.mjs` extension. Ensure your Node.js version is compatible (Node.js 12.20.0 or higher for v4.x).","cause":"Using `import` syntax for `mimic-response` v4.0.0+ in a Node.js file that is not treated as an ES module.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Use the correct default import syntax: `import mimicResponse from 'mimic-response';`.","cause":"Incorrectly attempting to import `mimicResponse` as a named export (e.g., `import { mimicResponse } from 'mimic-response';`) instead of a default export.","error":"TypeError: Cannot read properties of undefined (reading 'statusCode') OR TypeError: mimicResponse is not a function"},{"fix":"Implement a `destroy` method in the constructor options of your target stream (e.g., `new PassThroughStream({ destroy(error, callback) { /* ... */ } })`) that also calls `from.destroy(error)` if propagation is desired.","cause":"Calling `destroy()` on a target stream (`myStream`) without it having a custom `destroy` method, specifically when you intend for the destroy call to be propagated to the source stream.","error":"TypeError: myStream.destroy is not a function (when calling myStream.destroy())"}],"ecosystem":"npm"}