Chapter 20: Node.js Publish a Package

How to publish a package to npm in Node.js — written as if I’m sitting next to you, doing every step together on your laptop, explaining why we do each thing, what can go wrong, and what experienced developers actually do in 2025–2026.

Let’s publish a small but realistic utility package from zero to npm — step by step.

Goal of this tutorial

We’re going to create, test, prepare, and publish a small utility package called @webliance/date-helpers that contains a few useful date formatting functions.

You will learn:

  • How to structure a publishable package
  • The most important files (package.json, README, LICENSE, .npmignore…)
  • How to use scoped packages (@username/…)
  • How to publish safely (public vs private)
  • How to version and update packages
  • Common gotchas and best practices

Step 1 – Decide: scoped or unscoped?

Type Name example Who can publish updates? Most common in 2025–2026
Unscoped date-helpers Anyone who gets the name first Rare now (very competitive)
Scoped @webliance/date-helpers Only you (or your organization) Recommended — almost everyone uses scoped packages

Decision for this tutorial: We’ll publish a scoped package @webliance/date-helpers

Step 2 – Create the project folder & initialize

Bash

Now edit package.json — this is the most important file.

JSON

Important fields explained:

Field Why it matters Tip / Best practice
name Must be unique on npm — scoped = @yourname/… Use your npm username or org name
version Semantic versioning — starts at 1.0.0 Never publish 0.x.x as public packages
main / module / types Tells Node.js / bundlers / TypeScript where to find code Support both ESM + CommonJS + types
files Which folders/files to include in published package Usually only dist/ or lib/
exports Modern way to define entry points (replaces main in newer projects) Very important for dual ESM/CommonJS support
prepublishOnly Runs automatically before npm publish Perfect place to run build
publishConfig.access public or restricted Must be public for open-source
license Required — MIT is most common Choose MIT / ISC / Apache-2.0 for open-source

Step 3 – Create the actual code

src/index.ts

TypeScript

Step 4 – Add build step (very important)

We use tsup — one of the most popular tools in 2025–2026 for publishing libraries.

Bash

Create tsconfig.json

JSON

Now you can build:

Bash

This creates a clean dist/ folder with:

  • index.js (CommonJS)
  • index.mjs (ESM)
  • index.d.ts (TypeScript types)

Step 5 – Create essential files

README.md (very important — people read this first!)

Markdown

Usage

TypeScript

License

MIT

text

node_modules dist .env

text

src/ tests/ *.test.ts tsconfig.json

text

→ enter username, password, email

Check you’re logged in:

Bash

Should show your username

Step 7 – First publish!

Bash

You should see:

text

Now go to https://www.npmjs.com/package/@webliance/date-helpers

It’s live!

Step 8 – Updating the package (next versions)

Bash

Summary – Checklist before publishing

  • package.json has correct name, version, main, module, types, exports
  • “private”: false or missing (not true)
  • “publishConfig”: { “access”: “public” } (for scoped packages)
  • files or .npmignore — only publish what’s needed
  • Good README.md with install & usage example
  • LICENSE file present
  • build script works
  • prepublishOnly runs build
  • You are logged in (npm whoami)
  • You ran npm publish –dry-run first

Bonus – Modern best practices 2026

  • Use tsup, unbuild, rollup, or vite for building
  • Add changeset or semantic-release for automated versioning
  • Use vitest / uvu for tests — run before publish
  • Publish beta versions with –tag beta
  • Use GitHub Actions to automate publish on tag / release

Would you like to go deeper into any part?

  • Setting up automated versioning & publishing with changesets
  • Publishing beta / canary versions
  • Making your package dual ESM + CommonJS perfectly
  • Adding tests that run before publish
  • Handling scoped organizations on npm
  • What to do when you need to deprecate or unpublish versions

Just tell me which direction you want — I’ll continue with concrete examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *