TExcelTAD

Publishing to a Registry

Publishing turns a folder into an immutable name@version tarball on a registry, where any package manager can install it. This lesson publishes to the course's local Verdaccio registry, which behaves like npmjs.com for everything that matters here, and then installs the package back into a separate project.

Before you publish: pack

npm pack builds the exact tarball npm publish would upload, so you can inspect it first. Most publishing mistakes (a missing dist/, a leaked .env, 40 MB of test fixtures) are visible right here:

bash · try it yourself
mkdir -p slug/{src,test} && cd slug
cat > package.json <<'JSON'
{
  "name": "@learner/slug",
  "version": "1.0.0",
  "description": "Turn a title into a URL slug",
  "main": "src/index.js",
  "files": ["src"],
  "license": "MIT",
  "dependencies": { "lodash": "^4.17.21" }
}
JSON
echo 'const { kebabCase } = require("lodash"); module.exports = (s) => kebabCase(s);' > src/index.js
echo 'assert(slug("Hello World") === "hello-world")' > test/slug.test.js
echo 'SECRET=hunter2' > .env
echo '# @learner/slug' > README.md
npm pack
tar -tzf learner-slug-1.0.0.tgz
npm notice
npm notice 📦  @learner/slug@1.0.0
npm notice Tarball Contents
npm notice 16B README.md
npm notice 211B package.json
npm notice 79B src/index.js
npm notice Tarball Details
npm notice name: @learner/slug
npm notice version: 1.0.0
npm notice filename: learner-slug-1.0.0.tgz
npm notice package size: 362 B
npm notice unpacked size: 306 B
npm notice shasum: db049f4a68a91eed7409f3cde594165fd7612ad3
npm notice integrity: sha512-+8/aBOl07LWEw[...]t1ae+E66q/WRA==
npm notice total files: 3
npm notice
learner-slug-1.0.0.tgz
package/src/index.js
package/package.json
package/README.md

Only src/, package.json and README.md went in: files is an allow-list, and that's safer than an .npmignore deny-list, which silently ships every new file you forget to list. Every path in the tarball starts with package/.

Publish and install it back

bash · try it yourself
mkdir -p slug/src && cd slug
cat > package.json <<'JSON'
{ "name": "@learner/slug", "version": "1.0.0", "main": "src/index.js", "files": ["src"], "license": "MIT",
  "dependencies": { "lodash": "^4.17.21" } }
JSON
echo 'const { kebabCase } = require("lodash"); module.exports = (s) => kebabCase(s);' > src/index.js
npm whoami
npm publish 2>&1 | grep -E 'Publishing|^\+'
npm view @learner/slug name version dependencies dist.tarball
mkdir ../blog && cd ../blog && npm init -y > /dev/null
npm install @learner/slug 2>&1 | grep added
node -e 'console.log(require("@learner/slug")("Publishing to a Registry"))'
npm ls --all
learner
npm notice Publishing to http://localhost:4873/ with tag latest and default access
+ @learner/slug@1.0.0
name = '@learner/slug'
version = '1.0.0'
dependencies = { lodash: '^4.17.21' }
dist.tarball = 'http://localhost:4873/@learner/slug/-/slug-1.0.0.tgz'
added 2 packages in 696ms
publishing-to-a-registry
blog@1.0.0 /home/learner/blog
└─┬ @learner/slug@1.0.0
  └── lodash@4.18.1

The consumer got lodash too, because it was in the library's dependencies, and it got 4.18.1: the library's own lockfile (if it had one) plays no part, the consumer resolved ^4.17.21 fresh. npm view reads the published metadata back, including the tarball URL that will appear in every consumer's lockfile.

Versions are immutable

Once name@version is published, its content can never change. Publishing again with the same version fails; fix the bug, bump the version, publish again:

bash · try it yourself
mkdir -p slug && cd slug
echo '{ "name": "@learner/slug", "version": "1.0.0", "main": "index.js" }' > package.json
echo 'module.exports = 1;' > index.js
npm publish > /dev/null 2>&1
echo 'module.exports = 2;' > index.js
npm publish 2>&1 | grep -E '^npm error (code|403|.*cannot)' | head -2
npm version patch --no-git-tag-version
npm publish 2>&1 | grep '^+'
npm error You cannot publish over the previously published versions: 1.0.0.
v1.0.1
+ @learner/slug@1.0.1

On npmjs.com this goes further: even after npm unpublish, the same version number can never be reused, and unpublishing is only allowed within 72 hours and when nothing depends on the package. Treat every publish as permanent; use npm deprecate to warn people off a bad release instead.

Scoped packages, access and publishConfig

  • Scoped names (@learner/slug) are private by default on npmjs.com (a paid feature) and need npm publish --access public the first time, or "publishConfig": { "access": "public" }.
  • publishConfig overrides config for publish only, e.g. "registry": "https://npm.acme.internal/" so an internal package can never be published to the public registry by accident.
  • "private": true blocks publishing completely.
json
{
  "name": "@acme/units",
  "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" }
}

Publishing from CI, with provenance

Publishing from a laptop means a long-lived token on that laptop. The modern setup publishes only from CI:

  • Trusted publishing: npmjs.com is configured to trust one GitHub Actions workflow (or GitLab pipeline) and exchanges the job's short-lived OIDC identity for a publish token. No NPM_TOKEN secret exists at all.
  • Provenance: npm publish --provenance (automatic with trusted publishing) attaches a signed statement saying which repository, commit and workflow built this tarball. The registry shows it on the package page, and npm audit signatures verifies it on the consumer side.
yaml
# .github/workflows/release.yml (excerpt)
permissions:
  id-token: write        # lets the job request an OIDC token
  contents: read
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with: { node-version: 24, registry-url: "https://registry.npmjs.org" }
  - run: npm ci
  - run: npm test
  - run: npm publish --provenance --access public

Neither can run against the local registry: provenance needs a CI identity provider and a transparency log (the Supply Chain Security course goes deeper).

A pre-publish checklist

  1. npm pack --dry-run: only the files you mean to ship?
  2. Does the packed tarball work? npm install ../slug/learner-slug-1.0.0.tgz in a scratch project, then import it.
  3. version bumped according to semver?
  4. prepublishOnly runs the tests; prepack builds dist/.
  5. main/exports/types point at files that are in the tarball.

Test yourself

What is the safest way to control which files are published?

  1. An .npmignore deny-list
  2. The files allow-list in package.json
  3. Deleting files before publishing
  4. .gitignore

You published 1.0.0 with a bug. What do you do?

  1. Publish 1.0.0 again with the fix
  2. Publish 1.0.1 and deprecate 1.0.0
  3. Edit the tarball on the registry
  4. Delete the lockfiles of your users

What does npm provenance prove?

  1. That the package has no vulnerabilities
  2. Which repository, commit and CI workflow built the published tarball
  3. That the author is verified by npm staff
  4. That the tarball is minified