Programmatic API for the bits behind npm pack
npx @tessl/cli install tessl/npm-libnpmpack@9.0.0libnpmpack is a Node.js library for programmatically packing tarballs from local directories or from registry/GitHub specifications. It serves as the programmatic API behind the npm pack command, enabling developers to generate compressed .tgz archives without using the command-line interface.
npm install libnpmpackconst pack = require('libnpmpack');const pack = require('libnpmpack');
// Pack from current directory
const tarball = await pack();
// Pack from a local directory
const localTar = await pack('/path/to/my-package');
// Pack from a registry spec
const registryTar = await pack('abbrev@1.0.3');
// Pack from a GitHub spec
const githubTar = await pack('isaacs/rimraf#PR-192');
// Pack with options - write to disk (must provide packDestination)
const tarball = await pack('file:.', {
dryRun: false,
packDestination: '/tmp',
silent: true
});libnpmpack is built around several key components:
npm-package-arg to parse various package specifications (local, registry, GitHub)pacote to resolve package manifests and fetch package data@npmcli/run-script for local directory packing@npmcli/arborist for dependency tree analysisCreates npm package tarballs from various sources including local directories, registry specifications, and GitHub repositories.
/**
* Packs a tarball from a local directory or from a registry or github spec
* @param {string} [spec='file:.'] - Package specification to pack
* @param {PackOptions} [opts={}] - Options for packing
* @returns {Promise<Buffer>} - Promise that resolves to tarball data Buffer with attached metadata
*/
async function pack(spec = 'file:.', opts = {});interface PackOptions {
/** If explicitly false, writes tarball to disk (default: undefined, no writing) */
dryRun?: boolean;
/** Directory path where to write the tarball file (required when dryRun is false) */
packDestination?: string;
/** If true, skips prepack/postpack lifecycle scripts (default: false) */
ignoreScripts?: boolean;
/** If true, runs scripts with inherit stdio instead of pipe (default: false) */
foregroundScripts?: boolean;
/** Suppresses output (default: false) */
silent?: boolean;
/** Additional pacote options (registry, cache, etc.) */
[key: string]: any;
}
interface TarballBuffer extends Buffer {
/** Source specification that was packed */
from: string;
/** Resolved package identifier */
resolved: string;
/** Integrity hash of the tarball */
integrity: string;
}spec (string, optional)
'file:.''file:.' (current directory) or '/path/to/directory''package-name@version' or 'package-name''user/repo#branch-or-tag'opts (object, optional)
{}dryRun (boolean)
undefined (no file writing)false to write tarball to diskfalse, creates file named ${name}-${version}.tgzpackDestination (string)
undefineddryRun: false (will throw error if undefined)ignoreScripts (boolean)
falsetrue to skip prepack/postpack lifecycle scriptsforegroundScripts (boolean)
falsetrue to run scripts with inherit stdio instead of pipesilent (boolean)
falseReturns a Promise that resolves to a Buffer containing the tarball data. The Buffer has additional properties:
from: Source specification that was packedresolved: Resolved package identifierintegrity: Integrity hash of the tarballFor local directory packing (when spec.type === 'directory' and !opts.ignoreScripts):
prepack: Executed before tarball creation
opts.foregroundScriptspostpack: Executed after tarball creation
opts.foregroundScriptsnpm_package_from: tarball.fromnpm_package_resolved: tarball.resolvednpm_package_integrity: tarball.integrityopts.dryRun === false)opts.dryRun = false and provide packDestination${manifest.name}-${manifest.version}.tgz@/ with -@angular/core → angular-core-1.0.0.tgzpath.resolve(opts.packDestination, filename)packDestination is required when dryRun: false - will throw TypeError if undefinedThe function may throw errors in the following cases:
dryRun: false but packDestination is undefinedignoreScripts: true)const pack = require('libnpmpack');
// Pack current directory (no file written)
const tarball = await pack();
console.log(`Packed ${tarball.from}, size: ${tarball.length} bytes`);
// Pack and write to disk (packDestination is required)
const tarball = await pack('file:.', {
dryRun: false,
packDestination: './dist'
});
// Pack from registry with custom registry
const registryTar = await pack('lodash@4.17.21', {
registry: 'https://registry.npmjs.org/'
});
// Pack from GitHub
const githubTar = await pack('lodash/lodash#4.17.21');
// Pack with script handling
const tarball = await pack('/path/to/my-package', {
dryRun: false,
foregroundScripts: true, // Show script output
packDestination: './builds'
});
// Pack without running lifecycle scripts
const tarball = await pack('file:.', {
ignoreScripts: true,
silent: true
});