Pacote: JavaScript Package Downloader

21.5.0 · active · verified Sun Apr 19

Pacote is a robust JavaScript library designed for programmatically fetching package manifests and tarballs, primarily from the npm registry but also supporting various other package specifiers like Git repositories, local directories, and tarball URLs. It is the underlying package fetching mechanism used by the npm CLI itself, ensuring high compatibility with npm's ecosystem. The current stable version is 21.5.0, released in March 2026, and the project demonstrates an active release cadence with frequent updates across major versions (e.g., v19, v20, v21 receiving simultaneous updates). Key differentiators include its ability to resolve any npm-compatible package specifier, simulate packument data for non-registry sources, and run `prepare` scripts for Git or directory-based packages to replicate the publishing process. It provides APIs for resolving package URLs, extracting contents to a directory, fetching manifests, and downloading tarball data as buffers or streams.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates fetching a package manifest, extracting a package from a Git repository, and downloading a tarball directly from a URL using `pacote`'s core asynchronous APIs.

const pacote = require('pacote');
const path = require('path');
const fs = require('fs/promises');

async function main() {
  const packageName = 'lodash';
  const packageVersion = '4.17.21';
  const gitSpec = 'github:npm/cli';
  const tarballUrl = 'https://registry.npmjs.org/is-odd/-/is-odd-3.0.1.tgz';

  console.log(`--- Fetching manifest for ${packageName}@${packageVersion} ---`);
  try {
    const manifest = await pacote.manifest(`${packageName}@${packageVersion}`);
    console.log(`Got manifest for ${manifest.name}@${manifest.version}`);
    console.log(`Dependencies: ${Object.keys(manifest.dependencies || {}).join(', ') || 'None'}`);
  } catch (error) {
    console.error(`Failed to fetch manifest: ${error.message}`);
  }

  console.log(`\n--- Extracting ${gitSpec} to a temporary path ---`);
  const tempExtractPath = path.join(__dirname, 'temp-extracted-repo');
  try {
    await fs.mkdir(tempExtractPath, { recursive: true });
    const { from, resolved, integrity } = await pacote.extract(gitSpec, tempExtractPath);
    console.log(`Extracted '${from}' (resolved to '${resolved}') with integrity '${integrity}' to '${tempExtractPath}'`);
    const files = await fs.readdir(tempExtractPath);
    console.log(`Extracted files (first 5): ${files.slice(0, 5).join(', ')}...`);
  } catch (error) {
    console.error(`Failed to extract package: ${error.message}`);
  } finally {
    await fs.rm(tempExtractPath, { recursive: true, force: true });
  }

  console.log(`\n--- Downloading tarball from ${tarballUrl} ---`);
  try {
    const tarballData = await pacote.tarball(tarballUrl);
    console.log(`Got ${tarballData.length} bytes of tarball data. Resolved URL: ${tarballData.resolved}`);
  } catch (error) {
    console.error(`Failed to download tarball: ${error.message}`);
  }
}

main().catch(console.error);

view raw JSON →