npm Registry Fetch Client

19.1.1 · active · verified Tue Apr 21

npm-registry-fetch is a Node.js library providing a fetch-like API for interacting with npm registry APIs. It abstracts away complexities like selecting the correct registry, handling package scopes, and managing authentication details based on standard npm configuration. This package is the modern successor to npm-registry-client. Its current stable version is 19.1.1, and it maintains a consistent, active release cadence with frequent patch and minor updates, and occasional major version bumps primarily for Node.js engine compatibility or internal API refinements. A key differentiator is its deep integration with npm's ecosystem, ensuring correct behavior for features like cache revalidation for write operations, which makes it an authoritative choice for programmatic interaction with the npm registry.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `npm-registry-fetch` to make a basic GET request to the npm registry's ping endpoint, handling both raw `Response` objects and using the `fetch.json` utility for direct JSON parsing.

import npmFetch from 'npm-registry-fetch';

async function fetchPing() {
  try {
    // Basic fetch request to the npm registry ping endpoint
    const res = await npmFetch('/-/ping', {
      // Optional: Specify a custom registry, defaults to npmjs.org
      // registry: 'https://registry.npmjs.org/',
      // Optional: Pass an AbortSignal for request cancellation
      // signal: AbortSignal.timeout(5000),
      // Optional: Authentication token (e.g., from process.env)
      // token: process.env.NPM_TOKEN ?? '',
    });
    console.log('Status:', res.status);
    console.log('Headers:', res.headers.raw());
    const data = await res.json(); // Standard Fetch API method to parse JSON body
    console.log('Body (parsed JSON):', data);
  } catch (error) {
    console.error('Failed to fetch via raw response:', error.message);
  }

  try {
    // Using the convenience method fetch.json for direct JSON parsing
    const jsonBody = await npmFetch.json('/-/ping');
    console.log('\nUsing npmFetch.json for ping:', jsonBody);
  } catch (error) {
    console.error('Failed to fetch via .json helper:', error.message);
  }
}

fetchPing();

view raw JSON →