patch-package
patch-package copied to clipboard
Better support for monorepos
🐛 bug report
On a lerna-based monorepo, there are some issues with patch-package.
Suppose I have this folder structure...
.
├── ...
├── packages
│ ├── a
│ └── patches
│ └── package.json
├── node_modules
└── package.json
... and suppose I put patch-package dependency in the package a
together with a postinstall
script.
🤔 Current Behavior
The package a
should work in isolation from the top-level folder and other packages.
- When I do a
npm i
on the top-level folder, it will install dependencies and so do a npm i ona
(if things are well configured). But probably it willhoist
dependencies ofa
in the top-levelnode_modules
instead of a local one (ie. instead of anode_modules
inside thea
folder). - So
patch-package
will be executed and tries to patch things ina/node_modules
but since dependencies are hoisted in the top-level folder, it will fail (if no symlink are present).
💁 Possible Solution
Add a flag to enable patching of the top-level node_modules
. ie:
- it's very easy to find the root folder, just do a
find-up
for apackage.json
that contains aworkspaces
field and/or alerna.json
file. - From there, if you can't patch the local
node_modules
folder (ie. for instance it doesn't exist) tries to patch the rootnode_modules
(when flag is enabled)
🔦 Context
- I have a monorepo and I really don't want to put the patches in the root folder, but rather in the packages, so patch-package can work for packages individually.
- Also in my setup there are no local
node_modules
ie. nonode_modules
inpackages/a
and I would like patch-package to work out-of-the-box for this kind of situation (with a flag if it's ok). - I don't want to patch
patch-package
🌍 Your Environment
Software | Version(s) |
---|---|
patch-package | 6.2.2 |
+1
Experiencing the exact same issue. this becomes even more needed since npm 7 workspaces is out
+1
Any update on this? Currently running into this same issue, its a pain.
Any workarounds? @paeolo ?
As a hacky workaround, I was able to add this to my postinstall.
"postinstall": "cp -r ../../package_name node_modules/package_name && patch-package",
@tappin-kr I didn't use it yet, but I planned to use Yarn 2 and it seems it can handle patches itself without the need for an external package like patch-package
@paeolo Yes, sure!! but how about those who are not planning on migrating to Yarn 2 just yet(such as myself)? Monorepo is the way to go these days and it would be nice to have patch-package supports it.
Hello, anything new about this issue ?
I am wondering if this is an issue with patch-package
or if this could be solved if and when npm would
support the nohoist
option?
- https://github.com/npm/rfcs/issues/287
my solution: https://gist.github.com/0x0a0d/a22bdb57c736c8f0adc9209295aff7ed
Any updates or modern workarounds on this? The solution that @0x0a0d gave isn't relevant to me as I'm using the new version of lerna that simply uses workspaces.
I also have the same problem and my solution is to set the "--path-dir" as the relative path value after my "patch-package" command.
having the same issue, using turbo repo.
Just a note for anyone else possibly facing this with a docker-based setup: in our dockerfile we intentionally ran npm install with --ignore-scripts which meant that our patches were'nt applied. Also had to make sure the patches folder was actually copied into the container
+1
+1
+1
Workaround this issue:
- Place command
"patch:packageA": "patch-package"
in rootpackage.json
- Place hook command in
packageA
folder"postinstall": "cd ../ && npm run patch:packageA"
@sergeyshmakov your solution worked! Thanks a lot, you saved me a lot of time 🎉