Twelve Ways to Make Your Package Smaller to Install
Install size is a cost you pass to every user, every CI run and every container build. It is rarely one big mistake; it is a dozen small ones. Here are twelve tips, each with the one-line reason it matters. Measure before and after, so you know which ones paid off for your project.
Control what goes into the artefact
- Use an allowlist. Set
"files": ["dist"]inpackage.json, or the equivalent include rules in your Python build backend. Why: new files are excluded by default instead of leaking in. - Check the list every release. Run
npm pack --dry-runor list the wheel's contents. Why: it shows the true size and file count in seconds. - Leave tests and fixtures out. Why: test data is often the largest folder in a repository and no user runs your tests from
node_modules. - Decide on source maps deliberately. Why: they help debugging but can double the size of the build output; ship them if users need them, not by accident.
- Do not publish both minified and unminified builds without reason. Why: bundlers in consuming apps minify anyway, so a library rarely needs to ship both.
Trim the dependency tree
- Audit runtime dependencies. Run
npm ls --omit=devorpip showon each requirement. Why: transitive dependencies usually outweigh your own code. - Replace tiny utilities with platform features. Why: modern runtimes include things that once needed a package, such as
structuredClone,fetchandArray.prototype.flatin recent Node.js. - Move build tools to dev dependencies. Why: a compiler or bundler listed in
dependenciesis installed for every user. - Make heavy integrations optional. Use Python extras (
pip install example-lib[s3]), optional peer dependencies in npm, or Cargo features. Why: users who do not need the integration do not pay for it.
[project.optional-dependencies]
s3 = ["boto3>=1.28"]
plots = ["matplotlib>=3.7"]
Handle binaries and data with care
- Ship platform-specific wheels or packages for native code. Why: one wheel per platform means each user downloads only their binary, rather than all of them.
- Avoid downloading large assets in install scripts. Why: postinstall downloads break offline and locked-down environments, and many teams disable install scripts entirely.
- Fetch big datasets at runtime, on request. Why: a model file or dataset most users never touch should not be part of every install; provide a documented function to download and cache it.
How to measure the result
Record three numbers per release: the packed artefact size, the unpacked size and the total installed size including dependencies in a fresh project. The first two come from npm pack output or the wheel file. The third needs a clean install in an empty directory followed by a disk usage check. Compare like with like, on the same runtime and platform, and note the method in your changelog if you advertise an improvement. For a fuller method, see the install time benchmark notes and bundle size measurement.