Naming Your Package and Configuring package.json
Publishing to npm takes one command, so many first releases ship with broken imports, missing types or stray test fixtures in the tarball. Getting your package name and package.json right before version one avoids painful breaking migrations later.
Scoped vs unscoped names
Packages are either unscoped (for a made-up date helper, parse-window) or scoped to a user or organization (@acme/parse-window). Per npm's Package name guidelines and package.json docs, names must be 214 characters or fewer (including the scope), lowercase only, URL-safe and free of trademark conflicts. Avoid core Node.js module names and redundant js or node suffixes. Unscoped names cannot begin with a dot or underscore or mimic existing packages.
Unscoped names are shorter to type, while scoped names guarantee availability and group related packages cleanly. Under npm's scoped public packages guide, scoped packages default to private visibility, so pass npm publish --access public on your first publish or set "publishConfig": { "access": "public" }.
Key package.json fields
exportsandtypes: define your public entry points, block accidental deep imports into internal files, and point TypeScript to your declaration files.files: allowlists files packed into the tarball (for example["dist"]); omitting it defaults to["*"]. Per thenpm publishdocs,package.json,README,LICENSE,mainandbinare always included while.git,node_modules,.npmrcand lockfiles are excluded by default. Always inspect the tarball withnpm pack --dry-runfirst.sideEffects: per webpack's tree-shaking guide, set"sideEffects": falsefor pure libraries (or list side-effectful globs like["*.css"]) so bundlers can prune unused exports safely.repository,license,keywordsandengines: matchrepository.urlexactly to your GitHub or GitLab repo (required for provenance and Trusted Publishers), set an SPDX license, and declare your supported Node.js range.
Shipping ESM, CommonJS and TypeScript Declarations
Module resolution bugs cause more day-one issues than runtime code. Choose ESM-only ("type": "module") if your users run modern Node.js LTS and bundlers, or ship dual ESM and CommonJS if teams still load libraries via require().
For dual builds, follow Node.js's Conditional exports guide. Map "import" and "require" to separate .mjs and .cjs entry points from most specific to least specific with "default" last, keeping a top-level "main" fallback for older tools.
Per TypeScript's Declaration Files Publishing guide, bundle .d.ts declarations inside your package via "declaration": true (emitting matching .d.mts and .d.cts files for dual packages) whenever you own the source. And per TypeScript's Modules Reference, condition order is strict:
- Put
"types"first inside each branch ofexports(for example"import": { "types": "./dist/index.d.mts", "default": "./dist/index.mjs" }and the same for"require"). If JavaScript matches before"types", TypeScript falls back toany. - List any external
@types/*packages exposed in your public types underdependencies(notdevDependencies), and omit/// <reference path="..." />directives. - Test the real tarball locally by running
npm packand installing the.tgzinto separate ESM and CommonJS scratch projects with TypeScript strict mode enabled.
Trusted Publishers (OIDC), Provenance and 2FA
Modern npm releases replace long-lived publish secrets with short-lived CI identity tokens. Compare the three publish methods:
| Method | Authentication | Best fit | Provenance |
|---|---|---|---|
| Trusted Publishers (OIDC) | Short-lived CI identity token | GitHub Actions, GitLab CI or CircleCI releases | Automatic on GitHub Actions and GitLab CI (public repos) |
| Granular Access Token | Scoped, expiring token from npmjs.com | Read-only private installs or unsupported CI | Needs --provenance on supported CI |
| Local publish + 2FA | Interactive login + security key / OTP | Initial setup or manual stage approvals | Not available locally |
Follow npm's Trusted publishing guide for automated CI. It requires npm CLI 11.5.1+ and Node.js 22.14.0+ on cloud-hosted runners for GitHub Actions, GitLab CI/CD or CircleCI (self-hosted runners are not yet supported):
- Add the trusted publisher on npmjs.com: under package Settings > Trusted publishing (up to 10 per package), enter your case-sensitive GitHub org/user, repo name, workflow filename (just
publish.yml, not the full path), optional environment and allowed actions (npm stage publish,npm publishornpm dist-tag). Complete your first publish within 2 days or the unvalidated config expires. - Configure GitHub Actions: set
permissions: id-token: writeandcontents: read, run onubuntu-latest, passregistry-url: 'https://registry.npmjs.org'withpackage-manager-cache: false, matchpackage.json'srepository.urlto your repo, and runnpm publishwith no staticNPM_TOKEN. - Ship automatic provenance: per npm's provenance docs, publishing a public package from a public repo via GitHub Actions or GitLab CI Trusted Publishers automatically signs Sigstore provenance attestations into the public transparency log (no
--provenanceflag needed; CircleCI and private repos do not currently emit provenance). Consumers verify withnpm audit signatures. - Harden 2FA and tokens: under npm's 2FA policy, publishing requires 2FA (security keys or biometrics, defaulting to authorization and writes) or a bypass-2FA granular token (since August 2026, bypass-2FA tokens cannot run account-identity or governance actions). Once OIDC works, set Publishing access to Require two-factor authentication and disallow tokens (OIDC still runs), revoke old write tokens, or use stage-only CI (
npm stage publish) requiring 2FA approval. If CI installs private dependencies, issue a read-only granular access token on npmjs.com with an expiration date (at least 1 day ahead), scoped to needed packages (up to 50) and optional CIDR IP rules.
Semantic Versioning, Dist-Tags and Unpublish Rules
Version numbers are a compatibility contract. Follow Semantic Versioning 2.0.0:
0.1.0vs1.0.0: use0.y.zduring rapid early iteration; release1.0.0once teams depend on a stable public API.MAJOR.MINOR.PATCH: bumpPATCHfor backward-compatible fixes,MINORfor backward-compatible features or deprecations, andMAJORfor breaking changes (always deprecate in a minor release before removing in a major).- Pre-releases and dist-tags: ship release candidates like
2.0.0-rc.1withnpm publish --tag nextso normal installs stay onlatest. (OIDC dist-tag commands require npm CLI 11.21.0+ or 12.2.0+ withAllow npm dist-tagenabled.)
Document each release clearly using our guide on how to write a changelog. Also remember npm's Unpublish Policy: published version numbers are permanently immutable and can never be reused. You can only unpublish within 72 hours if no public packages depend on yours, or after 72 hours only with zero dependents, under 300 weekly downloads and a single maintainer (and unpublishing every version blocks new publishes for 24 hours). Otherwise, run npm deprecate <pkg>@<version> "reason" and publish a fixed patch.
README Quick Start and Developer Distribution
Your README.md is your npmjs.com product page. Put four items above the fold: one plain sentence stating the problem solved, the install command, a runnable code snippet with its real output, and links to docs, types and your license. For licenses, GitHub topics and contributor files, follow our open-source launch guide, and check your repo's share preview with the OG image previewer.
Registry search alone rarely brings early adopters. Launch where developers evaluate new libraries:
- Show HN: read our Show HN review and Show HN post guide, link your repo or live playground, and post a factual first comment explaining trade-offs and bundle size.
- Subreddits and newsletters: share technical write-ups in JavaScript, TypeScript or Node.js communities (following subreddit rules, see how to launch on Reddit) and submit notable releases to curated JavaScript weekly newsletters.
- Developer launch boards: list your docs or repo on DevHunt and Hauntboard. On Hauntboard, a person reviews every launch (free launches in about 72 hours, paid options within 24 hours), new listings spend 7 days in New this week before joining the all-time leaderboard, and page views, visits sent and votes stay public.
"Shipped package-name v1.0.0 on npm: a size/zero-dep TypeScript library for specific task. Install with npm i package-name. Built it because existing alternative struggled with specific pain point. Ships ESM + CJS with bundled types and npm provenance. Repo and runnable examples: URL"
Frequently Asked Questions
Once npm pack --dry-run is clean, your Trusted Publisher workflow is green and your README quick start runs from scratch, submit your npm package on Hauntboard alongside Show HN and DevHunt.
Should I publish my package as scoped or unscoped?
Pick unscoped if a short, clear name is free for a standalone library. Pick scoped (@org/pkg or @username/pkg) if the unscoped name is taken or you plan a suite of packages, and pass --access public on the first publish.
Do I need an npm access token in GitHub Actions to publish?
No. npm Trusted Publishers (OIDC) authenticates GitHub-hosted runners automatically when your workflow sets id-token: write on npm CLI 11.5.1+ and Node.js 22.14.0+. You only need a read-only granular token if CI installs private npm dependencies.
Can I overwrite or delete a broken npm version after publishing?
Never overwrite. You can unpublish within 72 hours if no public packages depend on it, or after 72 hours only with zero dependents, fewer than 300 weekly downloads and one maintainer. Even then, that version number stays retired, so npm deprecate plus a patch release is usually safer.
Why does TypeScript ignore my types when I add an exports field?
TypeScript evaluates conditional exports from top to bottom and stops at the first match. Always place "types" first inside each "import" and "require" branch so TypeScript resolves your .d.mts or .d.cts file before the JavaScript entry point.