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.
Register the plugin
Section titled “Register the plugin”npm i -D @unirelman/plugin-npmpnpm add -D @unirelman/plugin-npmyarn add -D @unirelman/plugin-npmbun add -d @unirelman/plugin-npmimport { 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.
Publish from your machine
Section titled “Publish from your machine”Log in once:
npm loginThen publish:
npx unirelman publishpnpm unirelman publishyarn unirelman publishbunx unirelman publishPublish from a monorepo
Section titled “Publish from a monorepo”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:
{ "packageManager": "pnpm@12.18.0"}Or set it in the configuration:
createNpmPlugin({ packageManager: "pnpm" });npm itself resolves neither specifier: with npm as package manager, a package that depends on workspace: or catalog: fails with NpmUnresolvedSpecifierError.
Publish from GitHub Actions
Section titled “Publish from GitHub Actions”With trusted publishing
Section titled “With trusted publishing”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.
-
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 allownpm stage publish.
A new trusted publisher configuration expires after 2 days without a successful publish.
-
Grant the job permission to request an identity token:
.github/workflows/release.yml permissions:contents: writepull-requests: writeid-token: write -
Run the workflow on a GitHub-hosted runner, the runner type npm trusted publishing supports.
-
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.
With a token
Section titled “With a token”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:
- 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 }}Configure the registry with actions/setup-node, and set the NODE_AUTH_TOKEN environment variable:
- 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 }}Set the YARN_NPM_AUTH_TOKEN environment variable:
- name: Release uses: MrSquaare/unirelman@v0.1.0-alpha.2 env: YARN_NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}Set the NPM_CONFIG_TOKEN environment variable:
- name: Release uses: MrSquaare/unirelman@v0.1.0-alpha.2 env: NPM_CONFIG_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:
{ "publishConfig": { "access": "public" }}Troubleshoot authentication
Section titled “Troubleshoot authentication”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.