clean-pkg-json
v2.1.0
Published
Clean package.json before publish by removing unnecessary properties
Maintainers
Readme
clean-pkg-json
Only publish necessary package.json properties.
clean-pkg-json removes development-only properties, such as devDependencies, most scripts, and tool configs, from package.json right before you pack or publish.
Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
Install
npm install -D clean-pkg-jsonSetup
Run it in the prepack script, after your build:
// package.json
{
"scripts": {
"prepack": "npm run build && clean-pkg-json",
},
}prepack runs on npm pack and npm publish (and the pnpm and Yarn equivalents), so the cleaned package.json is what gets published.
Good to know
- Build before cleaning. Run
clean-pkg-jsonafter the build, in the sameprepackscript. package.jsonstays cleaned. After a localnpm pack, restore it withgit checkout package.json.- Don't run it as a separate step before other tools. Use
prepack. If you must, keep what those tools need, for exampleclean-pkg-json --keep packageManager. - Don't restore
package.jsoninpostpack. It publishes the uncleaned metadata. - Preview the cleaned
package.jsonwithclean-pkg-json --dry. It prints the result and doesn't write the file. - Don't trust the numbers from
npm pack --dry-run.clean-pkg-jsondoesn't changepackage.jsonduring an npm dry run, so npm's file sizes, shasum, and integrity describe the original file.clean-pkg-jsonprints a note when this happens. For the real values, pack without--dry-run, then rungit checkout package.json. - With pnpm 11+ and Yarn 4, dry runs clean
package.jsontoo. Restore it afterward withgit checkout package.json.
- Build before cleaning:
exportsandimportsentries are removed when their files aren't in the package. Files that aren't built yet look missing, so their entries get removed. A warning is printed when this happens. package.jsonstays cleaned:clean-pkg-jsonrewritespackage.jsonin place and doesn't keep a copy.- Separate steps: every command after the cleanup sees the cleaned file. For example, without
packageManager, Corepack falls back to its default version, soclean-pkg-json && changeset publishin a Yarn 4 project runs changesets with Yarn 1. Inprepack, the publish tool has already started before the cleanup runs. postpack: npm readspackage.jsonagain afterpostpackto build the registry metadata. A restore there sends the original file, including all scripts, to the registry while the tarball has the cleaned one.- npm dry runs:
npm pack --dry-runandnpm publish --dry-runstill runprepackand build the tarball in memory.clean-pkg-jsondetects the dry run (npm_config_dry_run, which pnpm 10 also sets), prints the cleaned result, and leavespackage.jsonunchanged, so npm packs the original file. npm packs only thepackage.jsonon disk, so a dry run can't pack the cleaned file without changing it. Restoring the file afterward would need a separatepostpackstep, which doesn't run if the pack fails or is interrupted. - pnpm 11+ and Yarn 4: they don't tell lifecycle scripts about a dry run, so
clean-pkg-jsoncan't detect it and writes the file as in a real pack.
Example
Given this package.json:
{
"name": "my-package",
"version": "1.0.0",
"description": "A useful package",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "pkgroll",
"lint": "eslint .",
"test": "vitest",
"prepack": "npm run build && clean-pkg-json"
},
"dependencies": {
"lodash": "^4.0.0"
},
"devDependencies": {
"pkgroll": "^2.0.0",
"vitest": "^1.0.0",
"clean-pkg-json": "^2.0.0"
},
"eslintConfig": {
"extends": [
"@pvtnbr"
]
},
"lint-staged": {
"*.ts": "eslint --fix"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
}
}The published package.json is:
{
"name": "my-package",
"version": "1.0.0",
"description": "A useful package",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"dependencies": {
"lodash": "^4.0.0"
}
}Flags
| Flag | Description |
| - | - |
| -k, --keep <property name> | Keep more properties, e.g. -k eslintConfig or -k scripts.prepare. Accepts multiple flags or a comma-separated list. |
| -r, --remove <property name> | Remove properties that are kept by default, e.g. -r scripts.postinstall. Accepts multiple flags or a comma-separated list. |
| -d, --dry | Print the cleaned package.json without writing it. |
| -v, --verbose | Log what is kept and removed. |
| --published-only=false | Don't prune exports and imports. |
| -h, --help | Show help. |
| --version | Show version. |
How it works
- Properties: only properties that consumers use are kept. Everything else is removed.
- Scripts: only install hooks (
preinstall,install,postinstall) are kept, because they run when someone installs your package. If no scripts are left,scriptsis removed. exportsandimports: entries that point to files outside the published package are removed, so consumers can't resolve to files that don't exist. In conditional entries, only the unpublished branches are removed.
Default preserved properties
npm
nameversionprivatepublishConfigscripts.preinstallscripts.installscripts.postinstallgypfilebinary(prebuild-install, node-pre-gyp)filesbinbrowsermaindirectoriesmandependenciespeerDependenciespeerDependenciesMetabundleDependenciesbundledDependenciesoptionalDependenciesenginesoscpulibcdescriptionkeywordsauthormaintainerscontributorslicensehomepagerepositorybugsfunding
CDNs
Node.js
typeexportsimports
VSCode Extension Manifest
sponsorpublisherdisplayNamecategoriesgalleryBannerpreviewcontributesactivationEventsbadgesmarkdownqnaextensionPackextensionDependenciesextensionKindicon
Angular Package Format
fesm2020fesm2015esm2020es2020
TypeScript
typestypingstypesVersionstypescript(content mapper manifest, TypeScript 7.1+)
Bundlers (Webpack, Rollup, esbuild)
Agent Skills
This package ships with agent skills for AI coding assistants.
