NPM Registry Client (Forked and Maintained)
raw JSON →@qiwi/npm-registry-client is a fork of the original `npm-registry-client` package, providing a programmatic interface to interact with the npm registry. It was created to address and fix numerous vulnerabilities present in the upstream package and update its dependencies to a more current state (circa 2020), while also introducing TypeScript typings for improved developer experience. The current stable version is 8.9.1, though its last publish date was approximately five years ago, indicating a maintenance-oriented release cadence rather than active feature development. Key differentiators include its explicit focus on security fixes and type definitions compared to the original `npm-registry-client`, which is now largely unmaintained. It allows applications to fetch package metadata, handle authentication, and perform various registry operations such as retrieving package information.
Common errors
error Error: Cannot find module 'anonymous-npm-registry-client' ↓
npm install @qiwi/npm-registry-client or yarn add @qiwi/npm-registry-client, and change all import/require statements in your code to @qiwi/npm-registry-client. error TypeError: RegClient is not a constructor ↓
const RegClient = require('@qiwi/npm-registry-client'). For ESM/TypeScript, use import RegClient from '@qiwi/npm-registry-client' and ensure "esModuleInterop": true in your tsconfig.json. error Error: socket hang up / Error: read ECONNRESET / ETIMEDOUT ↓
RegClient constructor's config object, and consider increasing the timeout parameter in your request params. For persistent issues, try a different registry URL or consult network logs. Warnings
breaking This package, `@qiwi/npm-registry-client`, is a security-focused fork. The original `npm-registry-client` and any other unmaintained derivatives (like `anonymous-npm-registry-client`) are likely abandoned, vulnerable, and should NOT be used. Always ensure you are installing and importing `@qiwi/npm-registry-client`. ↓
gotcha The package is primarily published as a CommonJS module. While TypeScript projects can use ESM `import` syntax, direct ESM runtime support without a bundler or Node.js's `esModuleInterop` for CJS compatibility might be inconsistent as the `package.json` does not declare `module` or `exports` fields for native ESM. ↓
gotcha Proper error handling in callbacks is essential. Network requests can fail due to various reasons like timeouts, invalid URIs, or registry errors. The callback's `error` parameter should always be checked and handled. ↓
gotcha Authentication is critical for publishing packages, installing from private registries, or performing other protected registry operations. Omitting credentials (token, username/password, email) in the client's configuration object will result in unauthorized access errors for these actions. ↓
Install
npm install anonymous-npm-registry-client yarn add anonymous-npm-registry-client pnpm add anonymous-npm-registry-client Imports
- RegClient (CommonJS)
const RegClient = require('@qiwi/npm-registry-client') - RegClient (TypeScript ESM) wrong
import { RegClient } from '@qiwi/npm-registry-client'correctimport RegClient from '@qiwi/npm-registry-client' - RegClient (Type Import)
import type RegClient from '@qiwi/npm-registry-client'
Quickstart
import RegClient from '@qiwi/npm-registry-client';
const config = {
// Required for authenticated operations, e.g., publishing or private registry access.
// token: process.env.NPM_TOKEN ?? '',
// username: process.env.NPM_USERNAME ?? '',
// password: process.env.NPM_PASSWORD ?? '',
// email: process.env.NPM_EMAIL ?? '',
registry: 'https://registry.npmjs.org/', // Default npm registry URL
// cache: '/tmp/npm-cache', // Optional: path to cache directory
// proxy: { http: 'http://my.proxy.com', https: 'http://my.proxy.com' } // Configure proxy if needed
};
const client = new RegClient(config);
const packageName = 'react'; // Example: Fetch metadata for 'react'
const uri = `${config.registry}${packageName}`;
const params = { timeout: 5000 }; // Request timeout in milliseconds
client.get(uri, params, function (error, data, raw, res) {
if (error) {
console.error('Failed to fetch package data:', error.message);
if (error.statusCode === 404) {
console.error(`Package '${packageName}' not found. Check the package name and registry.`);
} else if (error.code === 'ETIMEDOUT') {
console.error('Request timed out. Consider increasing the timeout or checking network.');
}
return;
}
console.log(`Successfully fetched metadata for ${packageName}@${data['dist-tags'].latest}`);
console.log('Latest version description:', data.description);
// console.log('Raw JSON (truncated):', raw.substring(0, 200) + '...'); // The raw JSON string
// console.log('HTTP Response Status:', res.statusCode); // The full HTTP response object
});