You published a secret to npm. Do this, in this order.
A .env, an .npmrc with a token, a private key, or a key baked into dist/ went out with npm publish. Here's the order that matters.
1. Rotate the credential now
Assume it's already copied. Registry mirrors and CDNs (unpkg, jsDelivr, npmmirror) fetch new tarballs within minutes, and people do scrape new releases for keys. Unpublishing doesn't un-leak anything, so revoke and reissue before you touch npm:
- npm token:
npm token list, thennpm token revoke <id> - GitHub token: Settings → Developer settings → revoke it, then check the audit log
- AWS key: deactivate and delete it in IAM, then check CloudTrail for calls you didn't make
- Stripe, OpenAI, database URLs, webhook secrets: roll them in the provider dashboard
Once the old value is dead, the rest is cleanup.
2. Find every version that has it
The file is rarely in just the latest release. Check every published version. This pulls each tarball from the registry and looks for the filename, or for the leaked value itself:
pkg=your-package
needle='\.env' # filename regex, e.g. '\.env|\.npmrc|id_rsa'
value='sk_live_abc123' # or the leaked value itself (optional)
for v in $(npm view "$pkg" versions --json | jq -r 'if type=="array" then .[] else . end'); do
tgz=$(npm pack "$pkg@$v" --silent 2>/dev/null) || continue
if tar -tzf "$tgz" | grep -qE "(^|/)($needle)"; then echo "$v: file"; fi
if [ -n "$value" ] && tar -xzOf "$tgz" | grep -qF "$value"; then echo "$v: value"; fi
rm -f "$tgz"
doneFilename matching misses keys that a build step inlined into a bundle (for example process.env.X replaced at build time). Grepping for the value catches that, but only if you know the value. To find secrets you don't know about, scan the published package. It reads every file in the tarball, not just the names.
3. Unpublish, if npm will let you
npm's unpublish policy is strict. You can remove a version yourself only if:
- it was published less than 72 hours ago, or
- no other public package depends on it, it had fewer than 300 downloads in the last week, and it has a single owner/maintainer
npm unpublish your-package@1.4.2You can never reuse that version number. If you unpublish every version, the name is blocked for 24 hours. When the policy says no, npm support can still remove a version that contains a secret. Open a ticket at npmjs.com/support with the package, versions and what leaked. Rotate first anyway: removal can take days.
4. Ship a clean patch and deprecate the bad range
# fix packaging (step 5), then
npm version patch && npm publish
# warn anyone still installing a leaky version
npm deprecate "your-package@<1.4.3" "contained a leaked credential (rotated); upgrade to 1.4.3+"A deprecated version stays installable, but npm prints a warning on every install. Most people pin with ^, so they pick up the patch on their next install anyway.
5. Fix why it shipped
Almost always one of these:
- A
.npmignoreexists, so npm stopped reading.gitignoreand your gitignored.envwas back in scope. Details: the .npmignore trap. "files": ["dist"]and something wrote a secret or source map intodist/. Thefilesallowlist beats both ignore files. See source maps in npm packages.- Publishing from a laptop or a CI workspace with leftovers. npm packs the working directory, not the git commit, so files that were never committed still ship.
Switch to a "files" allowlist, then check the result before every publish with npm pack --dry-run.
6. Make the next one fail in CI
A dry-run list only helps if someone reads it. Gate the publish job instead. ReleaseShield runs npm pack, unpacks the tarball and fails the step on secrets or sensitive files before npm publish runs:
curl -fsSLo releaseshield.mjs https://release-shield-app.vercel.app/releaseshield.mjs
node releaseshield.mjs scanThe GitHub Action setup is in the docs. To check what's already out there, scan any published package. It's free and needs no login.