Skip to content

Publish to npm

This guide shows how to publish packages to the npm registry with the npm plugin. The plugin publishes with your package manager’s own command, so its configuration sets the registry and the authentication, as for a manual publish.

Terminal window
npm i -D @unirelman/plugin-npm
unirelman.config.ts
import { createNpmPlugin } from "@unirelman/plugin-npm";
// ...
plugins: [
// ...
createNpmPlugin(),
],

publish publishes each scope whose current version is missing from the registry, so running it again is safe.

Log in once:

Terminal window
npm login

Then publish:

Terminal window
npx unirelman publish

The plugin publishes each package with your package manager, so workspace: and catalog: specifiers resolve to versions in the published package. It reads the package manager from the packageManager field of your root package.json:

package.json
{
"packageManager": "pnpm@12.18.0"
}

Or set it in the configuration:

unirelman.config.ts
createNpmPlugin({ packageManager: "pnpm" });

npm itself resolves neither specifier: with npm as package manager, a package that depends on workspace: or catalog: fails with NpmUnresolvedSpecifierError.

Trusted publishing lets GitHub Actions publish with a short-lived identity token, and adds provenance for public repositories and packages. It works with npm 11.5.1 or later, on Node.js 22.14 or later, and with pnpm. Publish Bun packages with a token.

  1. On npmjs.com, open the settings of each package and add a trusted publisher:

    • Organization or user and Repository: your GitHub repository.
    • Workflow filename: the filename of your release workflow, with its extension, such as release.yml.
    • Allowed actions: allow npm publish, which the plugin runs; new configurations only allow npm stage publish.

    A new trusted publisher configuration expires after 2 days without a successful publish.

  2. Grant the job permission to request an identity token:

    .github/workflows/release.yml
    permissions:
    contents: write
    pull-requests: write
    id-token: write
  3. Run the workflow on a GitHub-hosted runner, the runner type npm trusted publishing supports.

  4. For a restricted package, also expose a read-only granular access token, as described in With a token: the plugin checks the registry with npm view, which trusted publishing doesn’t authenticate. npm still publishes with trusted publishing.

Create an npm access token and store it as a repository secret, such as NPM_TOKEN. Then expose the token to your package manager:

Configure the registry with actions/setup-node, and set the NODE_AUTH_TOKEN environment variable:

.github/workflows/release.yml
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: 24
registry-url: https://registry.npmjs.org
# ...
- name: Release
uses: MrSquaare/unirelman@v0.1.0-alpha.2
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

To add provenance, grant the job id-token: write, and set the environment variable of your package manager to true:

Package manager Environment variable
npm NPM_CONFIG_PROVENANCE
pnpm PNPM_CONFIG_PROVENANCE
yarn YARN_NPM_PUBLISH_PROVENANCE

Publish a scoped package for the first time

Section titled “Publish a scoped package for the first time”

npm publishes a scoped package, such as @example/core, as restricted by default. Make it public in its package.json:

package.json
{
"publishConfig": {
"access": "public"
}
}

When npm rejects the publish with ENEEDAUTH, E401, E403 or EOTP, the publish fails with an error such as:

error: Plugin "npm" failed in the createReleases phase: Failed to publish @example/core@1.0.1: npm could not authenticate or authorize the publish (E401)

The code comes from npm. Check that:

  • The token or the trusted publisher configuration exists and matches the package.
  • With trusted publishing: the workflow has id-token: write, its filename matches exactly, and it runs on a GitHub-hosted runner.
  • With 2FA required for publishing (EOTP): you publish with a granular access token or with trusted publishing.