Skip to main content
Publish a package to the npm registry.

Behavior

bun publish packages your project into a tarball and publishes it to the npm registry (or a private registry). It:
  1. Validates package.json (checks for required fields)
  2. Runs lifecycle scripts (prepare, prepublish, prepublishOnly)
  3. Creates a tarball of your package
  4. Uploads the tarball to the registry
  5. Runs postpublish scripts

Prerequisites

Authentication

You must be logged in to npm:
This stores credentials for publishing.

Required package.json fields

Your package.json must have:

Publishing modes

Publish from directory (default)

Automatically packs and publishes the current directory.

Publish a tarball

Publish a pre-built tarball.

Flags

--tag <tag>

Publish with a specific dist-tag (default: latest).
Common tags:
  • latest (default) - Stable release
  • next - Pre-release / canary
  • beta - Beta version
  • alpha - Alpha version
  • rc - Release candidate
Users can install tagged versions:

--access <public|restricted>

Set package access level.
Note: Unscoped packages must be public. Scoped packages (@org/package) can be public or restricted.

--dry-run

Simulate publishing without actually uploading to the registry.
Shows what would be published:

--otp <code>

Provide a one-time password for two-factor authentication.
If you have 2FA enabled and don’t provide --otp, Bun will prompt for it.

--no-ignore-scripts

Run lifecycle scripts (enabled by default). Use to explicitly enable scripts.

--ignore-scripts

Skip running lifecycle scripts.

--cwd <path>

Run command in specified directory.

Lifecycle scripts

Bun runs these scripts in order:
  1. prepare - Before packing, after npm install
  2. prepublishOnly - Before packing, only on bun publish
  3. prepack - Before tarball is created
  4. postpack - After tarball is created
  5. publish - After publishing (on success)
  6. postpublish - After publishing (on success)
Example in package.json:

Files included

By default, these files are included:
  • All files in the project
  • Respects .npmignore or files field in package.json

Using files field

Only specified files/directories are included.

Always included

  • package.json
  • README / README.md
  • LICENSE / LICENCE
  • CHANGELOG / CHANGELOG.md

Always excluded

  • .git
  • node_modules
  • .env files
  • .DS_Store

publishConfig

Configure publishing behavior in package.json:

Examples

Basic publish

Publish beta version

Publish with dry run

Publish with 2FA

Or provide OTP directly:

Publishing workflow

Typical workflow for publishing a new version:

Private packages

Publish to a private registry:

Scoped packages

Publish scoped packages:

Prevent accidental publishing

Mark a package as private to prevent publishing:
Attempting to publish will fail:

Verify published package

After publishing, verify:

Unpublishing

To unpublish a version:
Note: You can only unpublish within 72 hours. After that, contact npm support.

Common errors

Missing authentication

Run bunx npm login first.

Package already exists

Increment version with bun pm version patch.

Private package

Remove "private": true from package.json.

Unscoped package requires public access

Use --access public or scope the package.