%% generate tags start %% #software-engineering %% generate tags end %% #software-engineering/git #software-engineering/patching > If you are using pnpm, you should not use `patch-package` because pnpm comes with default patch command , see [[pnpm patch]] %% run start ```ts const {LinkPreview} = customJS return LinkPreview.getLinkPreviewFromUrl("https://github.com/ds300/patch-package") ``` %% <div class="nifty-link-card-container"> <a class="nifty-link-card" href="https://github.com/ds300/patch-package" target="_blank"> <div class="nifty-link-card-text"> <div class="nifty-link-card-title line-clamp-2">GitHub - ds300/patch-package: Fix broken node modules instantly 🏃🏽‍♀️💨</div> <div class="nifty-link-card-description">Fix broken node modules instantly 🏃🏽‍♀️💨. Contribute to ds300/patch-package development by creating an account on GitHub.</div> <div class="nifty-link-href"> <img class="nifty-link-icon" src="https://github.com/fluidicon.png"> https://github.com/ds300/patch-package </div> </div> <div class="nifty-link-image-container"> <div class="nifty-link-image" style="background-image: url('https://opengraph.githubassets.com/205e7663e9ce284bdb9e243895e01ddcaf17d529a8d52967abf27b056bcbdd65/ds300/patch-package')"> </div> </div> </a> </div> %% run end %% ## What is It? `patch-package` is a useful tool for making and keeping fixes to npm dependencies. It allows you to modify the code in `node_modules` and keep those changes even after reinstalling packages or updating them. ## How to Use (a Simple example)? ### 1. Choose a Repository For this example, let's assume we're working on a JavaScript project using the `express` library. You've found a minor issue in `express` that you want to fix temporarily until the maintainers address it. ### 2. Install `patch-package` and `postinstall-postinstall` First, you need to install `patch-package` and `postinstall-postinstall` in your project: ```bash npm install patch-package postinstall-postinstall ``` or if you're using Yarn: ```bash yarn add patch-package postinstall-postinstall ``` ### 3. Modify the Library Code Go to the `node_modules/express` directory in your project and make the necessary changes to the code. Let's say you made a small tweak in a file named `response.js`. ### 4. Create a Patch After making your changes, run the following command at the root of your project: ```bash npx patch-package express ``` This command creates a patch file in a directory named `patches` in your project root. The patch file contains all your changes. ### 5. Apply the Patch Automatically To ensure the patch is applied every time someone installs your project's dependencies, add the following in your `package.json`: ```json "scripts": { "postinstall": "patch-package" } ``` ### 6. Commit the Changes Commit the patch file along with your other project files. Anyone cloning your repository and running `npm install` or `yarn` will automatically have your patch applied to `express`. ### Example of a Patch File The patch file might look like this: ```diff diff --git a/node_modules/express/lib/response.js b/node_modules/express/lib/response.js index 9b9b9b9..9b9b9b9 100644 --- a/node_modules/express/lib/response.js +++ b/node_modules/express/lib/response.js @@ -42,7 +42,7 @@ module.exports = { var chunk = body; var encoding; var len; - var type = this.getHeader('Content-Type'); + var type = this.getHeader('Content-Type') || 'text/plain'; // adjust content-type if (typeof chunk === 'string') { ``` This patch file shows the exact change made to the `response.js` file in the `express` library. When applied, it will modify the `express` package in `node_modules` to include your changes. Remember, using `patch-package` is a temporary solution. You should aim to contribute the fix back to the original repository and revert to using the official version once your changes are included in a release. ## Alternatives and Comparison [[pnpm patch]]