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:
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.tgznpm 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
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 --alllearner
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:
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 neednpm publish --access publicthe first time, or"publishConfig": { "access": "public" }. publishConfigoverrides config forpublishonly, e.g."registry": "https://npm.acme.internal/"so an internal package can never be published to the public registry by accident."private": trueblocks publishing completely.
{
"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_TOKENsecret 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, andnpm audit signaturesverifies it on the consumer side.
# .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 publicNeither 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
npm pack --dry-run: only the files you mean to ship?- Does the packed tarball work?
npm install ../slug/learner-slug-1.0.0.tgzin a scratch project, then import it. versionbumped according to semver?prepublishOnlyruns the tests;prepackbuildsdist/.main/exports/typespoint at files that are in the tarball.
Test yourself
What is the safest way to control which files are published?
- An .npmignore deny-list
- The
filesallow-list in package.json - Deleting files before publishing
- .gitignore
You published 1.0.0 with a bug. What do you do?
- Publish 1.0.0 again with the fix
- Publish 1.0.1 and deprecate 1.0.0
- Edit the tarball on the registry
- Delete the lockfiles of your users
What does npm provenance prove?
- That the package has no vulnerabilities
- Which repository, commit and CI workflow built the published tarball
- That the author is verified by npm staff
- That the tarball is minified