[Question] patch on published module
Closed this issue · 4 comments
Hello,
currently I have a module project (named great-module) where the code is organized like that:
/node_modules/sub-module
/patches
package.json
From this repo, the postinstall script is correctly executed and the patch is applied to sub-module module.
After publishing my module (npm publish) I want to use it from another project (npm install geat-module).
But after install process, I have the following error message : Error: Patch file found for package sub-module which is not present at node_modules/sub-module
Indeed, after this installation, the code of this project is organized like that:
/node_modules/great-module/patches
/node_modules/sub-module
package.json
patch-package tries to use sub-module from /node_modules/great-module/node_modules/sub-module and not from /node_modules/sub-module.
What is the way to apply patches in published module?
Thanks a lot :)
Sorry, patch-package doesn't support publishing libraries. This has been asked before a number of times but it's not safe and therefore not something i want to encourage. If you really want to do this for some reason I'm gonna leave it up to you to figure out how to write a postinstall script that would work ;)
may you please elaborate more on "patch-package doesn't support publishing libraries" what do you mean exactly ? is this exposed by the readme / docs ?
I'm wondering if using prepare hook instead of postinstall would solve the issue. Do you see any drawback in using prepare ?
A simple solution
"postinstall": "cd $INIT_CWD && $(npm bin)/patch-package --patch-dir YOUR_PATCH_PATH"
I managed to fix this by making my published module (in my case, an executable) run patch-package based on how the package is installed - this migrated from using postinstall (install time) but instead running this at runtime
(more info: https://www.jvt.me/posts/2025/04/12/patch-package-distribute/)