How to Publish NPM Package to npmjs.com

2023/02/053 min read
bookmark this

Table of Contents

  1. Introduction
  2. Quick Release Commands
  3. Confirm npm Account and Scope Access
  4. Validate Package Metadata
  5. Build and Test Before Publish
  6. Verify What Will Be Published
  7. Bump Version
  8. Publish
  9. Verify Release
  10. Use the Helper Script
  11. Common Failure Cases

Introduction

This guide documents a safe release flow for publishing a scoped package to npm. It focuses on repeatable commands and validation steps before and after release.

Quick Release Commands

Use this sequence when you want a fast, repeatable release checklist:

npm login
npm whoami
npm install
npm run build
npm test
npm pack --dry-run
npm version patch
npm publish --access public
npm view @technoapple/ga4 version

Replace npm version patch with npm version minor or npm version major when needed.

Confirm npm Account and Scope Access

Make sure you are logged in and have permission for the @technoapple scope.

npm login
npm whoami

Validate Package Metadata

Check these fields in package.json before release:

  • name
  • version
  • main
  • typings
  • license

Because this is a scoped package (@technoapple/ga4), publish it with public access.

Build and Test Before Publish

Run install, build, and tests first:

npm install
npm run build
npm test

Verify What Will Be Published

Preview the package contents:

npm pack --dry-run

Confirm only expected files are included.

Bump Version

Choose one based on the type of change:

npm version patch
npm version minor
npm version major

This updates the package version and creates a Git tag.

Publish

For first publish (or any scoped public publish), run:

npm publish --access public

If 2FA is enabled for npm, you may be prompted for an OTP.

Verify Release

Check the published version:

npm view @technoapple/ga4 version

Confirm the npm registry shows the new version.

Use the Helper Script

You can also use the helper script in package.json:

npm run deploy

This runs build and then publishes with public access. It is still recommended to run tests and npm pack --dry-run before using deploy.

Common Failure Cases

  • You do not own the scope or package.
  • Version already exists (bump the version first).
  • Publishing to the wrong registry (check with npm config get registry).

Following this order helps reduce publish-time surprises and makes releases easier to repeat.