{"id":16195,"library":"responselike","title":"Mock HTTP Response Stream","description":"Responselike provides a streamable object designed to mimic a Node.js HTTP response stream (`http.IncomingMessage`). This utility is primarily used for mocking HTTP responses in testing scenarios or for reformatting cached data to be consumed by code expecting a standard HTTP response. The current stable version is 4.0.2. The package is actively maintained by Sindre Sorhus, with releases typically occurring as needed for bug fixes or Node.js version updates, as seen with the recent v4.0.0 and v4.0.1/v4.0.2 updates. Its key differentiator is its focus on accurately replicating the streamable nature and properties of a native HTTP response, making it suitable for integrations where stream-based consumption is expected, unlike simpler object mocks.","status":"active","version":"4.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/sindresorhus/responselike","tags":["javascript","http","https","response","mock","test","request","responselike","typescript"],"install":[{"cmd":"npm install responselike","lang":"bash","label":"npm"},{"cmd":"yarn add responselike","lang":"bash","label":"yarn"},{"cmd":"pnpm add responselike","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is pure ESM since v3.0.0, requiring `import` syntax. 'Response' is the default export.","wrong":"const Response = require('responselike');","symbol":"Response","correct":"import Response from 'responselike';"},{"note":"Type import for the constructor options object, useful for TypeScript users.","symbol":"ResponseOptions","correct":"import type { ResponseOptions } from 'responselike';"}],"quickstart":{"code":"import Response from 'responselike';\nimport { Writable } from 'stream';\n\nclass TestStream extends Writable {\n  _write(chunk, encoding, callback) {\n    console.log(chunk.toString());\n    callback();\n  }\n}\n\nconst response = new Response({\n\tstatusCode: 200,\n\theaders: {\n\t\t'content-type': 'text/plain',\n\t\t'x-custom-header': 'value'\n\t},\n\tbody: Buffer.from('Hello, World! This is a mocked response body.'),\n\turl: 'https://example.com/api/data'\n});\n\nconsole.log(`Status Code: ${response.statusCode}`);\nconsole.log(`URL: ${response.url}`);\nconsole.log('Headers:', response.headers);\n\nconst testOutput = new TestStream();\nconsole.log('\\nStreaming body content:');\nresponse.pipe(testOutput);\n","lang":"typescript","description":"Demonstrates creating a mock HTTP response, accessing its properties, and piping its body as a stream."},"warnings":[{"fix":"Upgrade your Node.js environment to version 20 or greater. If unable to upgrade, use `responselike@3.x` for Node.js 14+ or `responselike@2.x` for older Node.js versions.","message":"Version 4.0.0 and newer require Node.js version 20 or higher.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Migrate your codebase to use ESM `import` statements. Refer to Sindre Sorhus's 'pure ESM' gist for guidance on migrating projects. Ensure your `package.json` is configured correctly for ESM.","message":"Version 3.0.0 introduced breaking changes, requiring Node.js 14 and transitioning the package to pure ESM. CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Refactor calls from `new Response(statusCode, headers, body, url)` to `new Response({ statusCode, headers, body, url })`.","message":"Since v3.0.0, the `Response` constructor now accepts a single `options` object instead of multiple positional arguments.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Be aware that accessing headers should use lowercase keys, e.g., `response.headers['content-type']` even if originally provided as `{'Content-Type': '...'}`.","message":"When providing headers, keys will be automatically lowercased by the `Response` object to mimic standard HTTP header behavior.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Use `import Response from 'responselike';` instead of `const Response = require('responselike');`. Ensure your project is set up for ESM, potentially by adding `\"type\": \"module\"` to your `package.json`.","cause":"Attempting to `require()` responselike in an ESM project, or attempting to `require()` a pure ESM package in a CommonJS context.","error":"ERR_REQUIRE_ESM"},{"fix":"Ensure the `body` option is explicitly set to a `Buffer.from('your content')` when creating a new `Response` instance.","cause":"The `body` property passed to the `Response` constructor was not a `Buffer` or was missing, leading to an unstreamable object.","error":"TypeError: Cannot read properties of undefined (reading 'pipe')"},{"fix":"Correct the import statement to `import Response from 'responselike';` (for ESM) or verify your CommonJS wrapper if using older Node.js versions.","cause":"Incorrectly importing `Response` as a named export or attempting to use `new` on the module itself when it's a default export.","error":"TypeError: responselike_1.default is not a constructor"}],"ecosystem":"npm"}