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

  1. Use an allowlist. Set "files": ["dist"] in package.json, or the equivalent include rules in your Python build backend. Why: new files are excluded by default instead of leaking in.
  2. Check the list every release. Run npm pack --dry-run or list the wheel's contents. Why: it shows the true size and file count in seconds.
  3. 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.
  4. 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.
  5. 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

  1. Audit runtime dependencies. Run npm ls --omit=dev or pip show on each requirement. Why: transitive dependencies usually outweigh your own code.
  2. Replace tiny utilities with platform features. Why: modern runtimes include things that once needed a package, such as structuredClone, fetch and Array.prototype.flat in recent Node.js.
  3. Move build tools to dev dependencies. Why: a compiler or bundler listed in dependencies is installed for every user.
  4. 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

  1. 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.
  2. Avoid downloading large assets in install scripts. Why: postinstall downloads break offline and locked-down environments, and many teams disable install scripts entirely.
  3. 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.