urllib is a comprehensive HTTP client library for Node.js, designed to simplify URL interactions in complex scenarios. It provides features like basic and digest authentication, automatic redirection handling, configurable timeouts, and support for various request and response data types. The current stable version is 4.9.0, with a parallel maintenance branch for version 3 (latest 3.27.3). The library is built on top of Node.js's `undici` API, offering modern performance and adherence to web standards. It has a regular release cadence, with frequent bug fixes and minor feature additions across both major versions. Key differentiators include its robust handling of HTTP complexities often found in enterprise environments and its strong TypeScript support.
npm install urllibVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic GET request to a public API, parsing the response as JSON, and handling potential errors. It includes common options like `dataType`, `method`, `timeout`, and `headers`.
Review the official changelog for specific breaking changes between v3 and v4. Ensure your Node.js environment meets the `>=18.19.0` requirement. Test thoroughly after upgrading.
If your application relies on automatic decompression (e.g., `gzip`, `deflate`), explicitly set `compressed: true` in your request options. Otherwise, you might receive raw compressed data.
Always provide the `timeout` option as a number representing milliseconds (e.g., `timeout: 5000` for 5 seconds) to ensure consistent behavior across all `urllib` versions and avoid potential parsing issues.
Prefer `import { request } from 'urllib';` for making requests directly. If you need a custom `urllib` instance with specific configurations (e.g., a custom dispatcher), use `import { create } from 'urllib';`.Regularly run `npm audit` or `snyk test` in your project and update `urllib` to the latest recommended version, especially for patch releases, to ensure you have the most secure version.
For Node.js CommonJS files, change `import { request } from 'urllib';` to `const { request } = require('urllib');`. If you intend to use ESM, ensure your `package.json` has `"type": "module"` or files end with `.mjs`.Ensure you `await` the `request` call: `const { data, res } = await request(...);`. Remember that `await` can only be used inside `async` functions.Increase the `timeout` option in `request` (e.g., `timeout: 10000` milliseconds). Verify network connectivity and firewall rules. The target server might be down or heavily loaded.
Inspect the raw response content (e.g., by logging `data.toString()`) to understand why it's not valid JSON. You might need to remove `dataType: 'json'` and manually parse the `data` buffer/string after checking `res.headers['content-type']` or handle non-JSON responses gracefully.
No dependency data recorded yet.