Bun v1.0.0 --production flag and optionalDependencies pruning
0 reputation · 27 Dec 2022, 03:39 UTC
0 reputation · 27 Dec 2022, 03:39 UTC
Bun v1.0.0 introduces the --production flag for bun install to optimize deployment footprints by excluding devDependencies. This is particularly useful for reducing disk I/O and startup latency in containerized or serverless environments where hosting costs are tied to resource usage.
While the --production flag handles the removal of development dependencies, the current behavior regarding other dependency types is less restrictive. Specifically, optionalDependencies are installed by default, and peerDependencies are not automatically pruned during the production install process.
For workloads requiring the absolute minimum node_modules size to lower operational costs, it is unclear how to programmatically exclude optional dependencies without manually editing the package.json or employing external pruning scripts.
optionalDependencies when using the --production flag?peerDependencies that are not required by the production runtime?33275 reputation · 27 Dec 2022, 05:34 UTC
**1. Optional Dependencies** – Bun v1.0.0 does not expose a flag or configuration to skip optionalDependencies when running bun install --production. All optional dependencies listed in package.json are installed by default, and the current CLI options do not include a --no-optional or similar switch.
**2. Peer Dependencies** – Bun also does not automatically prune peerDependencies in a production install. Peer dependencies are treated the same as normal dependencies if they are present in package.json; otherwise they are not installed unless required by a transitive dependency. There is no built‑in command to strip them out.
While Bun lacks dedicated flags, you can still achieve a lean node_modules tree with the following routine:
Run a normal production install:
bun install --production
Identify optional and peer packages that are not needed by your runtime. A quick audit can be done with:
find node_modules -maxdepth 1 -type d -printf "%f\n" | grep -E "^optional|^peer"
Remove unwanted directories manually or via a script. For example:
rm -rf node_modules/optional-package
rm -rf node_modules/peer-package
Optionally run bun prune to clean up any extraneous packages that are no longer listed in package.json:
bun prune
For larger projects, consider a small Node or Bun script that parses package.json, removes entries under optionalDependencies and peerDependencies, writes the cleaned file, and then runs bun install --production again. This keeps your source intact while producing a minimal install.
bun install --help to confirm that no --no-optional flag exists.bun ls --depth=0 and compare against package.json to ensure no unwanted optional or peer packages remain.The Bun team has indicated that future releases may add flags such as --no-optional or automated peer pruning, but these are not present in v1.0.0. Staying on v1.0.0 means you’ll need to apply the manual approach described above.
Do you know if any of your modules rely on optional or peer dependencies at runtime? Identifying those will help you decide which packages can safely be removed.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.