Behavior
bun publish packages your project into a tarball and publishes it to the npm registry (or a private registry). It:
- Validates
package.json(checks for required fields) - Runs lifecycle scripts (
prepare,prepublish,prepublishOnly) - Creates a tarball of your package
- Uploads the tarball to the registry
- Runs postpublish scripts
Prerequisites
Authentication
You must be logged in to npm:Required package.json fields
Yourpackage.json must have:
Publishing modes
Publish from directory (default)
Publish a tarball
Flags
--tag <tag>
Publish with a specific dist-tag (default: latest).
latest(default) - Stable releasenext- Pre-release / canarybeta- Beta versionalpha- Alpha versionrc- Release candidate
--access <public|restricted>
Set package access level.
@org/package) can be public or restricted.
--dry-run
Simulate publishing without actually uploading to the registry.
--otp <code>
Provide a one-time password for two-factor authentication.
--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:prepare- Before packing, afternpm installprepublishOnly- Before packing, only onbun publishprepack- Before tarball is createdpostpack- After tarball is createdpublish- After publishing (on success)postpublish- After publishing (on success)
package.json:
Files included
By default, these files are included:- All files in the project
- Respects
.npmignoreorfilesfield inpackage.json
Using files field
Always included
package.jsonREADME/README.mdLICENSE/LICENCECHANGELOG/CHANGELOG.md
Always excluded
.gitnode_modules.envfiles.DS_Store
publishConfig
Configure publishing behavior inpackage.json:
Examples
Basic publish
Publish beta version
Publish with dry run
Publish with 2FA
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:Verify published package
After publishing, verify:Unpublishing
To unpublish a version:Common errors
Missing authentication
bunx npm login first.
Package already exists
bun pm version patch.
Private package
"private": true from package.json.
Unscoped package requires public access
--access public or scope the package.