TExcelTAD
Home › JS Packages & Bundlers: npm, Yarn, pnpm, Webpack, Rollup, esbuild › Toolkit: Inspecting Packages & Registries

Toolkit: Inspecting Packages & Registries

When a dependency misbehaves, guessing is slow. These are the tools that show you exactly what a registry serves, what a tarball contains, what changed between two versions, and what's in your local cache, plus how to run a private registry like the one this course uses.

Question Tool
What does the registry know about a package? npm view, or the registry's JSON API with curl
What's in a tarball before I install or publish it? npm pack --dry-run, npm pack pkg@version + tar -t
What changed between two versions? npm diff --diff=pkg@a --diff=pkg@b
Why is it installed / which versions? npm explain, pnpm why, yarn why, npm ls, npm query
Is my cache healthy? npm cache verify, pnpm store status
Which package manager and version does this repo use? packageManager field, Corepack
Can I host my own packages? Verdaccio, Artifactory, Nexus, GitHub Packages, GitLab

The registry API

npm registries speak a simple HTTP/JSON protocol. GET /<name> returns the packument: every version's manifest, the dist-tags and the tarball URLs. That's what every package manager downloads during resolution:

bash · try it yourself
curl -s http://localhost:4873/is-number | jq '{name, "dist-tags": .["dist-tags"], versions: (.versions | keys)}'
curl -s http://localhost:4873/is-number | jq '.versions["7.0.0"] | {main, engines, tarball: .dist.tarball, integrity: .dist.integrity}'
curl -s http://localhost:4873/-/ping && echo " <- ping"
npm view is-number@7.0.0 dist.fileCount dist.unpackedSize
{
  "name": "is-number",
  "dist-tags": {
    "latest": "7.0.0"
  },
  "versions": [
    "6.0.0",
    "7.0.0"
  ]
}
{
  "main": "index.js",
  "engines": {
    "node": ">=0.12.0"
  },
  "tarball": "http://localhost:4873/is-number/-/is-number-7.0.0.tgz",
  "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng=="
}
{}
 <- ping
dist.fileCount = 4
dist.unpackedSize = 9615

The integrity here is the same SRI hash that ends up in package-lock.json. Package managers request an abbreviated packument (Accept: application/vnd.npm.install-v1+json) that only contains the fields needed for installing, which matters for packages with thousands of versions.

Looking inside a tarball, and diffing versions

bash · try it yourself
mkdir -p look && cd look
npm pack is-number@7.0.0 2>/dev/null
tar -tzf is-number-7.0.0.tgz
echo "--- files that changed from 6.0.0 to 7.0.0"
npm diff --diff=is-number@6.0.0 --diff=is-number@7.0.0 --diff-name-only
npm diff --diff=is-number@6.0.0 --diff=is-number@7.0.0 -- package.json | grep '^[-+] ' | head -8
is-number-7.0.0.tgz
package/package.json
package/index.js
package/LICENSE
package/README.md
--- files that changed from 6.0.0 to 7.0.0
package.json
index.js
LICENSE
README.md
-  "description": "Returns true if the value is a number. comprehensive tests.",
-  "version": "6.0.0",
+  "description": "Returns true if a number or string value is a finite number. Useful for regex matches, parsing, user input, etc.",
+  "version": "7.0.0",
-    "node": ">=0.10.0"
+    "node": ">=0.12.0"
-    "benchmarked": "^2.0.0",
-    "chalk": "^2.1.0",

npm diff compares the published tarballs, not the git repository, which is exactly what you want when reviewing a dependency update: it shows what will actually run, including any code that never appeared on GitHub.

Caches

bash · try it yourself
mkdir -p c && cd c
npm init -y > /dev/null && npm install lodash@4.18.1 > /dev/null
npm cache verify | grep -E 'Content verified|Index entries'
ls ~/.npm/_cacache
echo '{ "name": "p", "version": "1.0.0" }' > ../p.json && mkdir ../p && mv ../p.json ../p/package.json
cd ../p && pnpm add lodash@4.18.1 > /dev/null && pnpm store status
Content verified: 2 (321703 bytes)
Index entries: 2
_lastverified
content-v2
index-v5
Packages in the store are untouched

npm's cache (~/.npm/_cacache) is content-addressable too, keyed by integrity hash; it just isn't used to link files into projects. Both caches are safe to delete; they only cost a re-download.

Running your own registry: Verdaccio

This course's registry is Verdaccio, an open-source Node.js registry that's a few lines of YAML away from useful. Its config here looks like this (uplinks is empty because there's no internet; normally it proxies npmjs.com and caches what it fetches):

bash · try it yourself
cat /opt/verdaccio/config.yaml
# Offline registry for build-js sessions: no uplinks, everything is served from the pre-seeded storage.
storage: /opt/verdaccio/storage
listen: 0.0.0.0:4873
auth:
  htpasswd:
    file: /opt/verdaccio/storage/htpasswd
uplinks: {}
packages:
  '@*/*':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
  '**':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
web:
  enable: false
server:
  keepAliveTimeout: 60
log: { type: stdout, format: pretty, level: warn }

A typical company setup:

yaml
uplinks:
  npmjs:
    url: https://registry.npmjs.org/
packages:
  '@acme/*':              # internal scope: never proxied, so no dependency confusion
    access: $authenticated
    publish: ci-bot
  '**':                   # everything else: proxied and cached from npmjs
    access: $all
    proxy: npmjs

Point npm at it with registry= or a scope mapping in .npmrc. Larger organisations use Artifactory, Nexus or their Git host's registry (GitHub Packages, GitLab Package Registry), which add retention policies, audit logs and other ecosystems (Maven, PyPI, Docker) in the same server.

Other tools worth knowing

Tool What it does
npm-check-updates (ncu) rewrites ranges in package.json to the latest versions, interactively
Renovate, Dependabot open dependency-update pull requests automatically, grouped and scheduled
publint, arethetypeswrong (attw) lint a package's exports/types before publishing
knip, depcheck find unused dependencies and files, and undeclared (phantom) ones
npm audit, pnpm audit, osv-scanner known vulnerabilities in the tree (need the internet)
Socket, npm audit signatures supply-chain signals: install scripts, new maintainers, provenance

Test yourself

What is a packument?

  1. A package's README
  2. The registry's JSON document for a package: all versions' manifests, dist-tags and tarball URLs
  3. The lockfile
  4. The tarball itself

Why review a dependency update with npm diff rather than the GitHub diff?

  1. GitHub is slower
  2. npm diff compares the published tarballs, i.e. the code that will actually run
  3. npm diff shows tests
  4. GitHub hides package.json

In a Verdaccio config, why is the internal @acme/* scope not proxied to npmjs?

  1. To save bandwidth
  2. So nobody can trick builds into installing a public package with the same name (dependency confusion)
  3. Because scopes can't be proxied
  4. To make publishing faster