← Today I Learned

Patch a node_modules dependency

If a library has a bug, there’s an easier solution than forking the repo: patch the dependency in your node_modules folder! If you’re using npm, you can install a package to do it, but pnpm has it built in:

pnpm patch <package-name>

When you run this, pnpm will generate a temporary folder (on macOS, it looks something like /private/var/folders/mg/longstringofchars/T/otherlongstringofchars) which contains the exact contents of the package’s node_modules.

You can change anything you want. I added "sideEffects": false to the package.json of lil-gui so esbuild could tree shake it out of production bundles, but changing code or type definitions will work just as well.

Once you’ve changed the file (or files!), run pnpm patch-commit <temporary-folder-path> and pnpm will generate a patch with your changes at patches/package@version.patch and modify your package.json and pnpm-lock.yaml. After those files are committed, pnpm will apply your changes every time you install your project’s dependencies.