{"id":17411,"library":"yakbak","title":"HTTP Request Recording and Playback Proxy","description":"Yakbak is a Node.js library for recording and playing back HTTP interactions, serving as a \"VCR\" for your HTTP clients during testing and development. It operates as a reverse proxy: requests are forwarded to a target host, and their responses are recorded (\"taped\") to disk. Subsequent identical requests are then played back from these recorded tapes instead of hitting the remote server, enabling consistent and fast test runs. Tapes are stored as `.js` files (Node modules), allowing for standalone replay servers. The current stable version is 5.0.1. While there isn't a strict release cadence, updates appear to be driven by dependency maintenance and Node.js version compatibility. Key differentiators include its reverse proxy approach, the flexible storage of tapes as re-executable Node modules, and its seamless integration with Node's native `http` module or popular frameworks like Express.","status":"active","version":"5.0.1","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/flickr/yakbak","tags":["javascript","http","https","record","playback","mock","service","vcr"],"install":[{"cmd":"npm install yakbak","lang":"bash","label":"npm"},{"cmd":"yarn add yakbak","lang":"bash","label":"yarn"},{"cmd":"pnpm add yakbak","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Yakbak exports a function directly (default export in CJS/ESM), which takes the target host and options, and returns an `http.Server` request handler function.","wrong":"const yakbak = require('yakbak').yakbak;","symbol":"yakbak","correct":"import yakbak from 'yakbak';"},{"note":"This is the traditional CommonJS import pattern commonly used in older Node.js projects or for direct `require` usage.","wrong":"import yakbak from 'yakbak';","symbol":"yakbak (CommonJS)","correct":"const yakbak = require('yakbak');"},{"note":"Individual recorded tapes are saved as Node.js modules (CommonJS) and can be required directly to create a server that replays a single, specific recorded response.","symbol":"Tape handler","correct":"const tape = require('./tapes/1117f3d81490d441d826dd2fb26470f9.js');"}],"quickstart":{"code":"const http = require('http');\nconst path = require('path');\nconst fs = require('fs');\nconst yakbak = require('yakbak');\n\n// Ensure the tapes directory exists\nconst tapesDir = path.join(__dirname, 'tapes');\nif (!fs.existsSync(tapesDir)) {\n  fs.mkdirSync(tapesDir, { recursive: true });\n}\n\n// Create a yakbak handler for a dummy API endpoint\n// We'll proxy to httpbin.org for demonstration\nconst handler = yakbak('https://httpbin.org', {\n  dirname: tapesDir\n});\n\n// Create an HTTP server that uses the yakbak handler\nconst proxyServer = http.createServer((req, res) => {\n  console.log(`Proxying request for: ${req.url}`);\n  handler(req, res); // Yakbak handles the request (record or playback)\n});\n\nconst PORT = 3000;\nproxyServer.listen(PORT, () => {\n  console.log(`Yakbak proxy server listening on http://localhost:${PORT}`);\n\n  // Example client request to the yakbak proxy\n  // This request will either be recorded or played back\n  http.get(`http://localhost:${PORT}/get?foo=bar`, (response) => {\n    let data = '';\n    response.on('data', (chunk) => (data += chunk));\n    response.on('end', () => {\n      console.log('\\n--- Client received response from Yakbak proxy ---');\n      console.log(`Status: ${response.statusCode}`);\n      console.log('Body:', JSON.parse(data));\n      proxyServer.close(() => {\n        console.log('Proxy server closed.');\n        // Clean up tapes for repeated runs, if not desired to persist\n        fs.rmdirSync(tapesDir, { recursive: true }); \n      });\n    });\n  }).on('error', (err) => {\n    console.error('Client request error:', err);\n    proxyServer.close();\n    fs.rmdirSync(tapesDir, { recursive: true });\n  });\n});","lang":"javascript","description":"This quickstart demonstrates setting up a Yakbak proxy server using Node's `http` module. It configures Yakbak to record or playback requests to `https://httpbin.org` and then makes a client request through the proxy to show the interaction, cleaning up the generated tapes afterwards."},"warnings":[{"fix":"Upgrade your Node.js environment to version 10 or higher to ensure compatibility.","message":"Yakbak v5.0.0 dropped support for Node.js versions older than 10. Users on unsupported Node.js runtimes will encounter errors or unexpected behavior.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Always specify a valid, writable directory path for `dirname` in the options object, e.g., `{ dirname: path.join(__dirname, 'tapes') }`.","message":"The `dirname` option is mandatory when initializing `yakbak(host, options)`. Failure to provide a valid path will result in errors during operation when the library attempts to save or load recorded tapes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be aware of the `noRecord` option's behavior. If you want requests to be recorded when no tape exists, ensure `noRecord` is `false` or omitted (as it defaults to `false`).","message":"When the `noRecord` option is set to `true`, Yakbak will return a 404 Not Found error for any request where a matching tape does not already exist on disk. It will *not* proxy the request to the target host and record it.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Provide the path to a recorded tape file that you wish to serve, for example: `yakbak ./tapes/your-tape-id.js`.","cause":"The `yakbak` command-line utility was executed without specifying the path to a recorded tape file as an argument.","error":"Error: file is required\nUsage: yakbak <file>"}],"ecosystem":"npm","meta_description":null}