# Publishing Packages & Private Registries (npm publish & .npmrc)

> Master packaging, authentication, scope routing, and safe publishing using npm publish, .npmrc, npm pack --dry-run, and provenance attestations.

Subject: Package Managers | Lesson 20 of 24 | 6 min read
Interactive lesson: https://codewithanimation.in/package-managers/publishing-and-private-registries
Previous: [Monorepos & Workspaces (npm, pnpm & Turborepo)](https://codewithanimation.in/package-managers/npm-workspaces-monorepos.md) | Next: [Security Audits & Supply Chain Attacks](https://codewithanimation.in/package-managers/npm-audit-and-supply-chain-security.md)

## What is Package Publishing and Private Registry Configuration?

Package publishing is the process of compressing your library source code, manifests, and documentation into a tarball archive (.tgz) and uploading it to a package registry for downstream consumption.

By default, npm publishes packages to the public npm registry. However, modern teams configure .npmrc files to route scoped packages (such as @myorg/core) to private enterprise registries like GitHub Packages, GitLab Registry, or self-hosted Verdaccio instances.

## Why Master Package Publishing and Scope Configuration?

Publishing reusable modules allows software teams to share UI components, design tokens, and utility functions across micro-frontends and internal services without duplicating code.

Understanding authentication tokens, scope mappings, and dry-run inspections prevents catastrophic credential leaks, breaks in deployment pipelines, and version collision errors during continuous integration publishing.

## How to Configure Scopes, Inspect Tarballs, and Execute npm publish

Map a scope such as @myorg to the intended registry with its @myorg:registry setting in .npmrc. Configure the target host's _authToken using an environment-variable reference such as ${GITHUB_TOKEN}; keep the actual credential outside source control.

Use files and npm's packaging rules to control the payload, and preview it with npm pack --dry-run. Publication access and provenance options depend on the registry and supported build environment. Provenance records origin information; it does not certify that package code is safe.

## Common mistake: Leaked Secrets, Version Collisions, and Unbuilt Source Artifacts

A severe mistake is relying on .npmignore instead of a package.json files whitelist, which frequently leads to accidental publishing of .env files, SSH keys, or test credentials to public registries.

Another common pitfall is version collision. Registries enforce immutable versions: attempting to re-publish an existing version returns HTTP 409 Conflict. Additionally, publishing uncompiled TypeScript code causes runtime SyntaxError failures for downstream consumers.

## Tip: Use prepublishOnly Hooks, Environment Tokens, and Provenance Flags

Add a `prepublishOnly` script in package.json to guarantee clean production builds before every publish command: `"prepublishOnly": "npm run build"`.

Lock registry targets directly inside package.json using `publishConfig`, and pass `--provenance` during publish to generate cryptographic Sigstore attestations linking your built tarball to its source repository commit.

## Try it: Interactive Package Publisher & Private Registry Simulator

Step through registry authentication, scope mapping, tarball dry-run inspection, and package publication across public and private registries.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/publishing-and-private-registries

## Check yourself

### Question 1

Which command is intended to preview the package payload without creating the tarball?

- A. npm publish
- B. npm pack --dry-run
- C. npm install -g
- D. npm audit fix --force

Correct answer: B

Dry-run packing reports what would be included. Review the contents before publishing; do not assume the entire repository or only dist is automatically the payload.

- Why not A: Publishing uploads a package instead of merely previewing it.
- Why not C: A global install is not a package-payload preview.
- Why not D: Audit remediation is unrelated to previewing a package archive.

### Question 2

In npm config, what does an @team:registry mapping select?

- A. Make every package on that registry public
- B. Compile @team packages into your application
- C. The registry used for packages in the @team scope
- D. Encrypt all files in your repository

Correct answer: C

A scope mapping selects the registry for that scope. Authentication remains separately configured for the appropriate registry host.

- Why not A: Routing does not change package visibility.
- Why not B: Registry configuration does not compile source.
- Why not D: A registry mapping is not a filesystem encryption setting.

### Question 3

Which authentication configuration is safer to commit as an example?

- A. A personal access token copied directly into .npmrc
- B. The production token embedded in package source code
- C. A token shared in a public issue for easier debugging
- D. A host-scoped token reference using an environment variable

Correct answer: D

Keep actual token values outside source control, scope credentials to the intended registry, and provide them through the deployment environment.

- Why not A: Committing the token exposes the credential.
- Why not B: Source publication could reveal that token.
- Why not C: A public issue is not an appropriate credential store.

## Remember this

- Use the package.json files whitelist array to explicitly control published payload files instead of relying solely on .npmignore.
- Map scoped packages to private registries in .npmrc using @myorg:registry=https://... and substitute authentication tokens via environment variables.
- Run npm pack --dry-run prior to publishing to inspect tarball contents and payload weight.
- Review package contents, access settings and supported provenance configuration before publishing; never commit actual token values.
