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

using turborepo / monorepo and npm

Open mdroidian opened this issue 11 months ago • 5 comments

Does patch-package support turborepo / monorepo using npm?

My repo has a similar structure to: https://github.com/vercel/turborepo/blob/main/examples/basic/README.md

Namely, each app / package (workspace) has a package.json but no package-lock.json, which gives me an error when trying to patch.

Steps to Repo

  • navigate to workspace cd apps/myApp
  • install patch-package npm i patch-package --save-dev
  • fix a bug in one of your dependencies vim node_modules/some-package/brokenFile.js
  • run patch-package to create a .patch file npx patch-package some-package
Creating temporary folder
Error: Can't find yarn.lock file

It is throwing this error because it cannot find the package-lock.json in the workspace directory, so it assumes yarn.

https://github.com/ds300/patch-package/blob/bd2e9a49d884516199079add143c1649541e8efe/src/detectPackageManager.ts#L44-L45

Patch Package in root

The package/file I want to patch is located in the node_modules of the app / package (workspace).

apps/myApp/nod_modules/some-package

Is it possible to target that via npx patch-package some-package when running in the root directory?

mdroidian avatar Jan 09 '25 01:01 mdroidian

Yes, it's possible. You can install patch-package and post-install at the root folder. Then, modify the file from the root/node_modules

I think you tried to install in the workspace instead of the root folder.

e.g.

root
├── /apps
│   ├── api
│   ├── db
│   └── web
├── /node_modules # Modify file here
├── /packages
│   └── your_awesome_package
├── package.json # Install patch-package & post-install in the root folder
├── ...
└── turbo.json

yongmin86k avatar Aug 04 '25 19:08 yongmin86k

Thank you for the response @yongmin86k

But sometimes the node_modules are in apps/my_app. In which case running npx patch-package apps/my_app/package_I_want_to_patch at root fails.

My workaround has been to add a package-lock.json to apps/my_app and run npx patch-package in apps/my_app

{
  "name": "dummy",
  "version": "1.0.0",
  "dependencies": {
    "//": "Dummy package so patch-package can run (to create a patch file).  Add the package you want to patch, with the correct version",
    "//": "https://github.com/ds300/patch-package/issues/551",
    "dummy-package": {
      "version": "1.0.0"
    },
    "@blueprintjs/datetime": {
      "version": "3.23.14"
    }
  }
}

mdroidian avatar Aug 04 '25 19:08 mdroidian

@mdroidian

Thanks for the workaround. I was in the exact same situation and your apply works perfectly.

In my case, the name field in package-lock.json had to match the name filed in package.json to work in ci using turbo.

vinitsood avatar Sep 02 '25 11:09 vinitsood

I have the same issue. While the workaround worked, I'm not happy at all using it.

Would be nice to have some way to run patch-package and to be able to specify which workspace should be patched.

mateuszbasiak avatar Oct 23 '25 12:10 mateuszbasiak

Exact same problem as https://github.com/ds300/patch-package/issues/551#issuecomment-3152096810. My workaround was to manually[*] create the patch file. Then npx patch-package correctly applies it. Far from ideal. 🤷

[*] Using Cursor

dalmotix avatar Oct 24 '25 00:10 dalmotix