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

support changing package.json

Open rifler opened this issue 3 years ago • 12 comments

Hi!

I make some changes in package.json file, add exports field

but patch-package doesn't see any changes in it

rifler avatar Aug 17 '20 12:08 rifler

does adding --exclude at the end of the command make it work?

hazem3500 avatar Aug 23 '20 16:08 hazem3500

Could patching dependencies changes in package.json be also supported? A package can have a dependency which is not really needed or a version which is not really required by the package and it can be downgraded. But it seems that patch-package doesn't allow to use patched dependencies at the moment.

inwaar avatar Sep 30 '20 13:09 inwaar

I tried @hazem3500's suggestion and while it did read the file changes and created a patch, applying the patch caused the following error

**ERROR** Failed to apply patch for package @cypress/webpack-preprocessor at path

Patches to package.json need to run before installing the package itself. For example, if you're changing a package's dependency version, you need to patch its package.json then npm install

I think one way to achieve this is by tagging those package.json patches then apply them via a preinstall hook

mwmcode avatar Oct 06 '20 03:10 mwmcode

#237 related

mwmcode avatar Oct 06 '20 03:10 mwmcode

@hazem3500 I previously tried with --exclude "" which didn’t work. I have no idea why --exclude would be any better, but it did the trick. Thanks

FlorianWendelborn avatar Dec 01 '20 21:12 FlorianWendelborn

@FlorianWendelborn you're welcome 😄

hazem3500 avatar Dec 02 '20 05:12 hazem3500

I think one way to achieve this is by tagging those package.json patches then apply them via a preinstall hook

@mwmcode but during that time the package does not exist yet? So I guess if you want to patch some dependency, you have to patch in postinstall and also do npm install again.. maybe with some few other tricks to force reinstall properly because the changes might not be picked up..


The patch I need to do in package.json is to force redirect the module prop to different path. My build process can't finish with esm-bundler dist file but it can finish with esm dist file for some reason.

I can generate the patch just fine, but the application files, because the package.json file is different for everyone on each install as npm or something does some in place replacements there.

I guess instead of package.json file I'll just switch the content of those two files altogether...

MartinMalinda avatar Apr 27 '22 10:04 MartinMalinda

is there a workaround for this?

sibelius avatar May 24 '22 11:05 sibelius

edit: sorry, i skipped

I tried @hazem3500's suggestion and while it did read the file changes and created a patch, applying the patch caused the following error

**ERROR** Failed to apply patch for package @cypress/webpack-preprocessor at path

Patches to package.json need to run before installing the package itself. For example, if you're changing a package's dependency version, you need to patch its package.json then npm install

I think one way to achieve this is by tagging those package.json patches then apply them via a preinstall hook

for me, the patch is applied, but changes to dependencies are ignored

The patch I need to do in package.json is to force redirect the module prop to different path.

this should work

off topic: creating patch for package.json via --exclude "" workaround

I previously tried with --exclude "" which didn’t work. I have no idea why --exclude would be any better, but it did the trick.

blame the buggy arg parser. fixed in my fork in https://github.com/milahu/patch-package/commit/bf8e1e7a80ceb17165be5ed0f82f44465d14d7c1

npm i -D @milahu/patch-package
npx patch-package cowsay --exclude ""
cat patches/cowsay*.patch
# generated by patch-package 6.4.12
#
# declared package:
#   cowsay: ^1.5.0
#
diff --git a/node_modules/cowsay/package.json b/node_modules/cowsay/package.json
index 7911c63..4fed97d 100644
--- a/node_modules/cowsay/package.json
+++ b/node_modules/cowsay/package.json
@@ -1,7 +1,7 @@
 {
   "name": "cowsay",
   "version": "1.5.0",
-  "description": "cowsay is a configurable talking cow",
+  "description": "cowsay is a configurable talking cow ... and stuff",
   "keywords": [
     "cow",
     "cowsay",

problem is, npm can modify the package.json file on install, so the patch can fail

so ideally, the patch file would be a javascript file, for example

// patches/cowsay+^1.5.0.patch.js

module.exports = [
  {
    file: "package.json",
    //file: /^package.json$/, // can be regex
    patch: (content) => {
      var pkg = JSON.parse(content);
      pkg.description += " ... and stuff";
      content = JSON.stringify(pkg, null, 2);
      return content;
    }
  }
  // can have more entries
]

should be easy to implement. PRs welcome : )

milahu avatar May 24 '22 12:05 milahu

for me, the patch is applied, but changes to dependencies are ignored

If you need to change dependencies, you probably want to use package.json resolutions instead of patch-package

FlorianWendelborn avatar May 24 '22 20:05 FlorianWendelborn

I think I've been back to this page at least 10 times. This is the worst default option I've ever seen, and patch-package has too many quirks in parsing arguments that --exclude= and --exclude='' don't even work.

BlackGlory avatar Oct 24 '22 15:10 BlackGlory

Interestingly, the difficulty seems to have increased further, now I have to use this: patch-package --exclude '^$'

BlackGlory avatar Dec 10 '23 10:12 BlackGlory