libnpmpack provides the programmatic interface for creating tarballs of npm packages, mirroring the functionality of the `npm pack` command-line tool. As an internal library of the npm CLI, it offers a robust and consistent way to prepare packages for distribution, including handling `.npmignore`, `.gitignore`, and package lifecycle scripts during the packing process. The current stable version of the `libnpmpack` package itself is 9.1.5, though it is often consumed as part of the broader npm CLI, which follows its own release schedule (e.g., v10.x and v11.x branches) and sees frequent updates. Its release cadence is tightly coupled with the npm CLI's monorepo development. A key differentiator is that it's the authoritative, low-level implementation for `npm pack`, ensuring compatibility with npm's official packing logic, unlike third-party alternatives which might deviate in edge cases. It focuses on encapsulating the complex logic of package bundling and file inclusion rules.
npm install libnpmpackVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `libnpmpack` to programmatically create an npm package tarball from a local directory, including `package.json` and custom files, and then clean up. It simulates a package project and uses the `pack` function with basic options.
Upgrade your Node.js environment to version 20.17.0 or higher, or 22.9.0 or higher.
Pin `libnpmpack` to exact versions in your `package.json` to prevent unexpected behavior with `npm install`, and review npm CLI release notes closely for any changes related to package packing behavior.
Thoroughly test your packing process, inspect the contents of generated `.tgz` files, and consult npm's official documentation on package content for precise control over what gets published.
Migrate your project to use ES Module `import` syntax: `import { pack } from 'libnpmpack';`. Ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions for your source files.Verify the import style. For `libnpmpack`, `pack` is a named export: `import { pack } from 'libnpmpack';`.Ensure the directory where the tarball is to be created has appropriate write permissions for the user running the Node.js process, or specify a different `packDestination` with adequate permissions.