{"id":17884,"library":"r2","title":"r2 HTTP Client","description":"The `r2` package is a minimalist, promise-based HTTP client library envisioned as a spiritual successor to the popular `request` package. It distinguishes itself by being built primarily upon the browser's Fetch API, with Node.js support provided through shims, resulting in a substantially smaller footprint, particularly for browser environments (e.g., 66KB uncompressed for `r2` versus 2MB for `request` when bundled). The library's API is designed for modern JavaScript development, leveraging `async/await` for asynchronous operations. The current stable version is 2.0.1, released in April 2018. Its release cadence has been effectively halted since then, indicating long-term dormancy. A primary differentiator is its Fetch API-first design, aiming for simplicity and efficiency over feature-rich complexity, making it a lightweight option for basic HTTP interactions.","status":"abandoned","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/mikeal/r2","tags":["javascript"],"install":[{"cmd":"npm install r2","lang":"bash","label":"npm"},{"cmd":"yarn add r2","lang":"bash","label":"yarn"},{"cmd":"pnpm add r2","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"As of v2, r2 is exclusively a CommonJS module. Direct ES module imports are not supported without transpilation or a CJS wrapper.","wrong":"import r2 from 'r2'","symbol":"r2 (main function)","correct":"const r2 = require('r2')"},{"note":"Named methods like get, post, put, etc., are properties of the main r2 object, not directly exported named functions.","wrong":"const { get } = require('r2'); await get('https://example.com')","symbol":"r2.get (or other methods)","correct":"const r2 = require('r2'); await r2.get('https://example.com').text"},{"note":"r2 is a function that returns a promise-like object directly for chaining, not a class to be instantiated with the 'new' keyword.","wrong":"new r2('https://example.com')","symbol":"r2() (function call)","correct":"await r2('https://example.com').text"}],"quickstart":{"code":"const r2 = require('r2');\n\nasync function fetchData() {\n  try {\n    // Fetch HTML content from Google\n    const html = await r2('https://www.google.com').text;\n    console.log('Google HTML (truncated):', html.substring(0, 100) + '...');\n\n    // Send a PUT request with JSON data and receive JSON response\n    const payload = { status: 'success', timestamp: Date.now() };\n    const jsonResponse = await r2.put('https://httpbin.org/put', { json: payload }).json;\n    console.log('HTTPBin PUT response:', jsonResponse);\n\n    // Fetch with custom headers\n    const headers = { 'x-custom-header': 'r2-test' };\n    const responseWithHeaders = await r2('https://httpbin.org/headers', { headers }).json;\n    console.log('HTTPBin Headers response:', responseWithHeaders.headers['X-Custom-Header']);\n\n  } catch (error) {\n    console.error('Error fetching data:', error);\n  }\n}\n\nfetchData();","lang":"javascript","description":"Demonstrates basic GET requests for text, PUT requests with JSON payloads, and GET requests with custom headers using r2's async/await interface."},"warnings":[{"fix":"Consult the v2.0.0 source code or documentation (if available) to understand the new API surface, as explicit migration guides are scarce.","message":"Version 2.0.0 introduced a complete overhaul of the API, explicitly stating \"MAJOR API BREAK OF EVERY METHOD AND THE HASHER.\" Code written for v1.x will not be compatible with v2.x without significant refactoring.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Evaluate alternatives like `node-fetch`, `axios`, or the native `fetch` API (with a polyfill for Node.js) for projects requiring active maintenance, security updates, and modern features. Use `r2` with caution in production, especially for new projects.","message":"The `r2` package has not received updates since April 2018. This means it may contain unpatched security vulnerabilities, outdated dependencies, or compatibility issues with newer Node.js versions or browser environments.","severity":"gotcha","affected_versions":">=2.0.1"},{"fix":"Use `const r2 = require(\"r2\")` for Node.js environments. For browser environments where ES Modules are preferred, consider bundling with tools like Webpack or Rollup, or use a modern HTTP client with native ESM support.","message":"`r2` is built on the Fetch API but is primarily a CommonJS module. It does not natively support ES Modules (import/export syntax) without a transpilation step (e.g., Babel, TypeScript) or a wrapper.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Wrap the code using `await` in an `async` function and call it. Example: `async function myFunc() { await r2(...); } myFunc();`.","cause":"Attempting to use `await` keyword outside of an `async` function or a module explicitly configured as an ES module.","error":"SyntaxError: await is only valid in async functions and the top level bodies of modules"},{"fix":"`r2` is a function that returns a promise-like object directly, not a class. Call it as `await r2('url').text`. Ensure `const r2 = require('r2')` is correctly defined and in scope.","cause":"Attempting to instantiate r2 using `new r2()`, or calling a method on an undefined `r2` variable.","error":"TypeError: r2 is not a function"},{"fix":"Verify the URL is correct and the target server is running and accessible from your environment. For 'localhost', ensure the server is listening on the correct port and address.","cause":"The hostname specified in the URL (e.g., 'localhost') could not be resolved by the DNS system, or the network connection is unavailable.","error":"Error: getaddrinfo ENOTFOUND localhost"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}