bun patch
Persistently patch node_modules packages in a git-friendly way
bun patch persistently patches packages in node_modules in a maintainable, git-friendly way.
Sometimes you need a small change to a package in node_modules/ to fix a bug or add a feature. bun patch lets you do this without vendoring the entire package.
Features:
- Generates
.patchfiles that Bun applies to dependencies innode_moduleson install - You can commit
.patchfiles to your repository and reuse them across installs, projects, and machines "patchedDependencies"inpackage.jsonkeeps track of patched packages. Bun only reads it from the rootpackage.json;bun installwarns when a workspace package declares it.- Patches packages in
node_modules/while preserving the integrity of Bun's Global Cache - Test your changes locally before committing them with
bun patch --commit <pkg> - To preserve disk space and keep
bun installfast, Bun commits patched packages to the Global Cache and shares them across projects where possible
Step 1. Prepare the package for patching
Use bun patch <pkg> to prepare the package for patching:
# you can supply the package name
bun patch react
# ...and a precise version in case multiple versions are installed
bun patch react@17.0.2
# or the path to the package
bun patch node_modules/react
Step 2. Test your changes locally
bun patch <pkg> makes it safe to edit <pkg> in node_modules/ directly, while preserving the integrity of Bun's Global Cache. It works by re-creating an unlinked clone of the package in node_modules/. bun patch --commit <pkg> then diffs that clone against the original package in the Global Cache.
Step 3. Commit your changes
Once you're happy with your changes, run bun patch --commit <path or pkg>.
Bun generates a patch file in patches/, updates your package.json and lockfile, and starts using the patched package:
# you can supply the path to the patched package
bun patch --commit node_modules/react
# ... or the package name and optionally the version
bun patch --commit react@17.0.2
# choose the directory to store the patch files
bun patch --commit react --patches-dir=mypatches
# `patch-commit` is available for compatibility with pnpm
bun patch-commit react
If bun install could not apply the resulting patch, or the install is refused (for example by a security scanner), the commit fails and changes nothing: your edits, package.json, the lockfile, and any earlier patch file stay as they were.
To run the same checks without writing anything, pass --dry-run. It prints the path of the patch file that the commit would write.
Removing a patch
Undo your changes and commit again:
bun patch react
# restore the files you changed in node_modules/react
bun patch --commit react
When the package has no changes left, Bun removes it from "patchedDependencies" in package.json and the lockfile, deletes its patch file unless another entry uses it, and reinstalls the original package.
You can also delete the entry from "patchedDependencies" and the patch file yourself, then run bun install.
A missing patch file
bun install and bun patch <pkg> fail when a file in "patchedDependencies" does not exist, and leave node_modules alone. While the patched package is still in node_modules, bun patch --commit <pkg> writes the file again. To start over, delete the entry from "patchedDependencies" and run bun install.
CLI Usage
bun patch <package>@<version>
Patch Generation
--commitbooleanInstall a package containing modifications in dir
--patches-dirstringThe directory to put the patch file in (only if --commit is used)
Dependency Management
--productionbooleanDon't install devDependencies. Alias: -p
--ignore-scriptsbooleanSkip lifecycle scripts for all packages, including the project's package.json and trusted dependencies
--trustbooleanAdd to trustedDependencies in the project's package.json and install the package(s)
--globalbooleanInstall globally. Alias: -g
--omitstringExclude dev, optional, or peer dependencies from install
Project Files & Lockfiles
--yarnbooleanWrite a yarn.lock file (yarn v1). Alias: -y
--no-savebooleanDon't update package.json or save a lockfile
--savebooleandefault trueSave to package.json (true by default)
--frozen-lockfilebooleanDisallow changes to lockfile
--save-text-lockfilebooleanSave a text-based lockfile
--lockfile-onlybooleanGenerate a lockfile without installing dependencies
Installation Control
--backendstringPlatform-specific optimizations for installing dependencies. Possible values: clonefile (default on
macOS), hardlink (default on Linux and Windows), symlink, copyfile
--linkerstringLinker strategy (one of isolated or hoisted)
--minimum-release-agenumberOnly install packages published at least N seconds ago (security feature)
--minimum-release-age-excludesstringPackages exempt from --minimum-release-age (comma-separated or repeated; replaces the bunfig.toml list)
--dry-runbooleanPrint what the command would do, with or without --commit, and leave the project as it is
--forcebooleanAlways request the latest versions from the registry & reinstall all dependencies. Alias: -f
--no-verifybooleanSkip verifying integrity of newly downloaded packages
Network & Registry
--castringProvide a Certificate Authority signing certificate
--cafilestringSame as --ca, but as a file path to the certificate
--use-system-cabooleanUse the system's trusted certificate authorities
--use-openssl-cabooleanUse OpenSSL's default CA store instead of the bundled one
--use-bundled-cabooleanUse bundled CA store
--registrystringUse a specific registry by default, overriding .npmrc, bunfig.toml, and environment
variables
--network-concurrencynumberdefault 64Maximum number of concurrent network requests (default 64)
Performance & Resource
--concurrent-scriptsnumberMaximum number of concurrent jobs for lifecycle scripts (default: 2x CPU cores)
Caching
--cache-dirstringStore & load cached data from a specific directory path
--no-cachebooleanIgnore manifest cache entirely
Output & Logging
--silentbooleanDon't log anything
--quietbooleanDisable the progress bar
--verbosebooleanExcessively verbose logging
--no-progressbooleanDisable the progress bar
--no-summarybooleanDon't print a summary
Platform Targeting
--cpustringOverride CPU architecture for optional dependencies (e.g., x64, arm64, * for
all)
--osstringOverride operating system for optional dependencies (e.g., linux, darwin, * for
all)
--libcstringOverride libc for optional dependencies (e.g., glibc, musl, * for all)
Global Configuration & Context
--configstringSpecify path to config file (bunfig.toml). Alias: -c
--cwdstringSet a specific current working directory
--env-filestringLoad environment variables from the specified file(s)
--no-env-filebooleanDisable automatic loading of .env files
Help
--helpbooleanPrint this help menu. Alias: -h