patch-package icon indicating copy to clipboard operation
patch-package copied to clipboard

Better support for monorepos

Open paeolo opened this issue 3 years ago • 18 comments

🐛 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 on a (if things are well configured). But probably it will hoist dependencies of a in the top-level node_modules instead of a local one (ie. instead of a node_modules inside the a folder).
  • So patch-package will be executed and tries to patch things in a/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 a package.json that contains a workspaces field and/or a lerna.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 root node_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. no node_modules in packages/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

paeolo avatar Nov 17 '20 14:11 paeolo

+1

Experiencing the exact same issue. this becomes even more needed since npm 7 workspaces is out

jony89 avatar Feb 10 '21 08:02 jony89

+1

KevinGorjan avatar Apr 22 '21 14:04 KevinGorjan

Any update on this? Currently running into this same issue, its a pain.

Any workarounds? @paeolo ?

tappin-kr avatar May 06 '21 21:05 tappin-kr

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 avatar May 06 '21 23:05 tappin-kr

@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 avatar May 08 '21 17:05 paeolo

@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.

wibb36 avatar Jun 06 '21 21:06 wibb36

Hello, anything new about this issue ?

JB-CHAUVIN avatar Jul 20 '21 09:07 JB-CHAUVIN

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

bd82 avatar Sep 14 '21 11:09 bd82

my solution: https://gist.github.com/0x0a0d/a22bdb57c736c8f0adc9209295aff7ed

0x0a0d avatar Sep 29 '21 17:09 0x0a0d

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.

NathanC avatar Mar 01 '23 04:03 NathanC

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.

tcguoxing avatar Jun 25 '23 06:06 tcguoxing

having the same issue, using turbo repo.

asp3 avatar Jul 28 '23 21:07 asp3

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

hrmJ avatar Sep 26 '23 06:09 hrmJ

+1

carlmagumpara avatar Oct 26 '23 02:10 carlmagumpara

+1

agusting19 avatar Nov 09 '23 15:11 agusting19

+1

leochen-g avatar Nov 16 '23 12:11 leochen-g

Workaround this issue:

  • Place command "patch:packageA": "patch-package" in root package.json
  • Place hook command in packageA folder "postinstall": "cd ../ && npm run patch:packageA"

sergeyshmakov avatar Dec 25 '23 10:12 sergeyshmakov

@sergeyshmakov your solution worked! Thanks a lot, you saved me a lot of time 🎉

Monte9 avatar Apr 09 '24 21:04 Monte9