Read contents of package tarball created by npm pack
npx @tessl/cli install tessl/npm-lerna--get-packed@5.6.0@lerna/get-packed provides functionality to read and analyze the contents of npm package tarballs created by npm pack. It extracts comprehensive metadata including file listings, sizes, checksums, and bundled dependencies from tar archives.
npm install @lerna/get-packedconst { getPacked } = require("@lerna/get-packed");ES modules (if your project supports them):
import { getPacked } from "@lerna/get-packed";const { getPacked } = require("@lerna/get-packed");
const path = require("path");
// Package metadata (typically from package.json)
const pkg = {
name: "my-package",
version: "1.0.0",
bundleDependencies: ["lodash"] // optional
};
// Path to tarball created by npm pack
const tarFilePath = "./my-package-1.0.0.tgz";
// Analyze the tarball
getPacked(pkg, tarFilePath).then(result => {
console.log(`Package: ${result.id}`);
console.log(`Size: ${result.size} bytes`);
console.log(`Unpacked size: ${result.unpackedSize} bytes`);
console.log(`Files: ${result.entryCount} entries`);
console.log(`SHA1: ${result.shasum}`);
console.log(`Bundled deps: ${result.bundled.join(", ")}`);
// List all files
result.files.forEach(file => {
console.log(`${file.path} (${file.size} bytes)`);
});
});Analyzes npm pack tarballs and extracts comprehensive metadata about package contents.
/**
* Analyzes npm pack tarball and returns comprehensive metadata
* @param {Object} pkg - Package metadata object
* @param {string} pkg.name - Package name
* @param {string} pkg.version - Package version
* @param {string[]} [pkg.bundleDependencies] - List of bundled dependencies
* @param {string[]} [pkg.bundledDependencies] - Alternative name for bundled dependencies
* @param {string} tarFilePath - Path to the tarball file created by npm pack
* @returns {Promise<PackageMetadata>} Promise that resolves to package metadata
*/
function getPacked(pkg, tarFilePath);Return Value:
interface PackageMetadata {
/** Package identifier in format "name@version" */
id: string;
/** Package name */
name: string;
/** Package version */
version: string;
/** Size of the tarball file in bytes */
size: number;
/** Total size of all entries when unpacked */
unpackedSize: number;
/** SHA1 hash of the tarball (hexadecimal) */
shasum: string;
/** SHA512 integrity object from ssri */
integrity: Object;
/** Basename of the tarball file */
filename: string;
/** Array of file objects */
files: FileEntry[];
/** Total number of entries in the tarball */
entryCount: number;
/** Array of bundled dependency names found in node_modules */
bundled: string[];
/** Original tarball file path */
tarFilePath: string;
}
interface FileEntry {
/** Relative path of the file (without package/ prefix) */
path: string;
/** File size in bytes */
size: number;
/** File mode/permissions */
mode: number;
}Usage Examples:
Basic tarball analysis:
const { getPacked } = require("@lerna/get-packed");
const pkg = { name: "example", version: "1.0.0" };
const result = await getPacked(pkg, "./example-1.0.0.tgz");
console.log(`${result.name}@${result.version}`);
console.log(`Tarball size: ${result.size} bytes`);
console.log(`Contains ${result.files.length} files`);Analyzing bundled dependencies:
const pkg = {
name: "my-app",
version: "2.1.0",
bundleDependencies: ["lodash", "moment"]
};
const result = await getPacked(pkg, "./my-app-2.1.0.tgz");
console.log(`Bundled dependencies found: ${result.bundled.join(", ")}`);File inspection:
const result = await getPacked(pkg, tarFilePath);
// Find large files
const largeFiles = result.files.filter(file => file.size > 10000);
console.log("Large files:", largeFiles.map(f => `${f.path} (${f.size} bytes)`));
// Check for specific file types
const jsFiles = result.files.filter(file => file.path.endsWith('.js'));
console.log(`JavaScript files: ${jsFiles.length}`);Error Handling:
The function returns a rejected Promise if:
Common error scenarios:
try {
const result = await getPacked(pkg, "./nonexistent.tgz");
} catch (error) {
if (error.code === 'ENOENT') {
console.error("Tarball file not found");
} else if (error.message.includes('TAR_BAD_ARCHIVE')) {
console.error("Invalid or corrupted tarball");
} else {
console.error("Unexpected error:", error.message);
}
}