Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for monorepos #277

Open
paeolo opened this issue Nov 17, 2020 · 18 comments
Open

Better support for monorepos #277

paeolo opened this issue Nov 17, 2020 · 18 comments

Comments

@paeolo
Copy link

paeolo commented Nov 17, 2020

πŸ› 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
@jony89
Copy link

jony89 commented Feb 10, 2021

+1

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

@KevinGorjan
Copy link

+1

@tappin-kr
Copy link

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

Any workarounds? @paeolo ?

@tappin-kr
Copy link

tappin-kr commented May 6, 2021

As a hacky workaround, I was able to add this to my postinstall.

"postinstall": "cp -r ../../package_name node_modules/package_name && patch-package",

@paeolo
Copy link
Author

paeolo commented May 8, 2021

@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

@wibb36
Copy link

wibb36 commented Jun 6, 2021

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

@JB-CHAUVIN
Copy link

Hello, anything new about this issue ?

@bd82
Copy link

bd82 commented Sep 14, 2021

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?

@0x0a0d
Copy link

0x0a0d commented Sep 29, 2021

@NathanC
Copy link

NathanC commented Mar 1, 2023

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.

@tcguoxing
Copy link

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.

@asp3
Copy link

asp3 commented Jul 28, 2023

having the same issue, using turbo repo.

@hrmJ
Copy link

hrmJ commented Sep 26, 2023

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

@carlmagumpara
Copy link

carlmagumpara commented Oct 26, 2023

+1

2 similar comments
@agusting19
Copy link

+1

@leochen-g
Copy link

+1

@sergeyshmakov
Copy link

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"

jcfranco added a commit to Esri/calcite-design-system that referenced this issue Feb 14, 2024
@Monte9
Copy link

Monte9 commented Apr 9, 2024

@sergeyshmakov your solution worked! Thanks a lot, you saved me a lot of time πŸŽ‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests