{"id":15985,"library":"clone-response","title":"Clone HTTP Response Stream","description":"The `clone-response` package provides a utility function to duplicate a Node.js HTTP response stream, effectively creating an unconsumed copy. This is particularly useful in scenarios where a response stream needs to be processed in multiple asynchronous contexts or passed to different consumers without fully draining the original stream for all parties. The package ensures that all properties and methods from the original `IncomingMessage` are copied to the new stream, creating a complete duplicate. As of version 2.0.0, it is a pure ESM module and requires Node.js 14 or higher. The package maintains a stable release cadence, primarily updating for Node.js compatibility and modern JavaScript module standards. Its core differentiator is the reliable duplication of a response stream's state and data, enabling flexible handling of HTTP responses without complex stream buffering or re-fetching.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/clone-response","tags":["javascript","clone","response","duplicate","copy","http","stream"],"install":[{"cmd":"npm install clone-response","lang":"bash","label":"npm"},{"cmd":"yarn add clone-response","lang":"bash","label":"yarn"},{"cmd":"pnpm add clone-response","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since version 2.0.0, `clone-response` is a pure ESM module and must be imported using `import` syntax. Attempting to `require()` it will result in a runtime error.","wrong":"const cloneResponse = require('clone-response');","symbol":"cloneResponse","correct":"import cloneResponse from 'clone-response';"},{"note":"`cloneResponse` is exported as the default export of the module. Using a named import will result in `undefined` or a module not found error depending on the environment.","wrong":"import { cloneResponse } from 'clone-response';","symbol":"cloneResponse (named import attempt)","correct":"import cloneResponse from 'clone-response';"},{"note":"In ESM contexts, it is best practice to use the `node:` protocol prefix for built-in Node.js modules like `http` to prevent naming collisions and enhance clarity.","wrong":"const http = require('http');","symbol":"http (Node.js built-in)","correct":"import http from 'node:http';"}],"quickstart":{"code":"import http from 'node:http';\nimport cloneResponse from 'clone-response';\n\nhttp.get('http://example.com', response => {\n\tconst clonedResponse = cloneResponse(response);\n\tresponse.pipe(process.stdout);\n\n\tsetImmediate(() => {\n\t\t// The response stream has already been consumed by the time this executes,\n\t\t// however the cloned response stream is still available.\n\t\t// In a real application, you would consume clonedResponse here.\n\t\tconsole.log('Cloned response is still available for consumption.');\n\t\t// Example: clonedResponse.on('data', chunk => console.log('Cloned data:', chunk.toString()));\n\t});\n});","lang":"javascript","description":"This example demonstrates how to clone an HTTP response stream immediately after receiving it, allowing the original to be piped to `process.stdout` while the cloned stream remains available for later asynchronous consumption."},"warnings":[{"fix":"Migrate your project to ESM using `import` statements or use an older version of the package if CommonJS is strictly required. Ensure your `package.json` has `\"type\": \"module\"` or use `.mjs` file extensions.","message":"`clone-response` is now a pure ESM (ECMAScript Module). CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade your Node.js runtime to version 14.16 or newer. If unable to upgrade, you must use `clone-response` version 1.x.x.","message":"Node.js 14.16 or higher is now required. Older Node.js versions are not supported.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Be mindful of stream consumption order. Clone the stream as early as possible. If you need to use the original stream in an async context, ensure you create a clone *before* any operations that might consume it asynchronously.","message":"The process of cloning a stream itself consumes the original stream in that immediate tick. While multiple clones can be made in the same tick, passing the original stream into any asynchronous callbacks after cloning will result in it being already consumed.","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":"Change your import statement from `const cloneResponse = require('clone-response');` to `import cloneResponse from 'clone-response';`. Ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`).","cause":"`clone-response` is an ES Module, but you are trying to import it using CommonJS `require()` syntax.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module [path/to/node_modules/clone-response/index.js] from [your_file.js] not supported."},{"fix":"Use the correct default import syntax: `import cloneResponse from 'clone-response';`.","cause":"You might be attempting to import `cloneResponse` as a named export (`import { cloneResponse } from 'clone-response';`) when it is the default export.","error":"TypeError: cloneResponse is not a function"},{"fix":"Update `const http = require('http');` to `import http from 'node:http';` to consistently use ESM import syntax with `node:` protocol for built-in modules.","cause":"While `clone-response` is ESM, you might be mixing CommonJS `require()` for built-in Node.js modules like `http` within an ESM file.","error":"Error: The 'http' module cannot be used in 'esm' context with 'require()'."}],"ecosystem":"npm"}