libnpmpublish provides the programmatic interface used internally by the npm CLI for publishing and unpublishing packages to a configured registry. As of the provided metadata, its stable version is 11.1.3, making it a current and actively maintained component within the broader npm ecosystem. The library is typically released as part of the npm CLI monorepo, suggesting a consistent and frequent release cadence aligned with the CLI's development. It is a critical tool for automating package lifecycle operations in Node.js environments, abstracting the complexities of package tarball creation, manifest handling, registry communication, and authentication. Its key differentiator is being the official, low-level API that ensures full compatibility with npm's internal logic and registry interaction protocols, making it the canonical choice for developers building tools that interact directly with the npm registry.
npm install libnpmpublishVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically publish a new npm package using `libnpmpublish`. It creates a temporary package directory, generates a `package.json` and a simple `index.js`, then uses `pacote` to obtain the manifest and tarball buffer. Finally, it calls the `publish` function with an authentication token (from `NPM_TOKEN` environment variable) and configuration options like access level. It includes error handling and cleanup.
Upgrade your Node.js environment to a compatible version (20.17.0+ or 22.9.0+).
Pass an `opts.token` (e.g., `process.env.NPM_TOKEN`) or ensure your execution environment has an `~/.npmrc` file with an `_authToken` entry for the target registry. For scoped packages published as public, `access: 'public'` must be set.
Ensure your package version is a valid semver increment. For pre-release versions (e.g., `1.0.0-beta.1`), always include `defaultTag: 'beta'` in the options to avoid errors.
Include `access: 'public'` in the options object passed to `publish` if you intend to make your scoped package publicly available.
Before calling `publish`, use `pacote.manifest()` to get the package manifest and `pacote.tarball.stream()` (and convert to Buffer) to get the tarball data from your package source.
Ensure an `opts.token` is provided to `publish` with a valid npm token, or that `npm login` has been executed in the environment and a token is available in the `~/.npmrc` file. For CI/CD, use environment variables for `NPM_TOKEN`.
Verify that the package name is unique or that you have publishing rights. If it's a new scoped package intended to be public, ensure `access: 'public'` is passed in the options.
Ensure you are using `pacote.manifest()` or similar to correctly parse and pass the package's `package.json` content as the first argument to `publish`.
Upgrade your Node.js runtime to a version compatible with `^20.17.0 || >=22.9.0`.