Skip to content

support changing package.json #250

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

Open
rifler opened this issue Aug 17, 2020 · 12 comments
Open

support changing package.json #250

rifler opened this issue Aug 17, 2020 · 12 comments

Comments

@rifler
Copy link

rifler commented Aug 17, 2020

Hi!

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

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

@hazem3500
Copy link

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

@inwaar
Copy link

inwaar commented Sep 30, 2020

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.

@mwmcode
Copy link

mwmcode commented Oct 6, 2020

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
Copy link

mwmcode commented Oct 6, 2020

#237 related

@FlorianWendelborn
Copy link

@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

@hazem3500
Copy link

@FlorianWendelborn you're welcome 😄

@MartinMalinda
Copy link

MartinMalinda commented Apr 27, 2022

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

@sibelius
Copy link

is there a workaround for this?

@milahu
Copy link
Contributor

milahu commented May 24, 2022

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 milahu@bf8e1e7

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 : )

@FlorianWendelborn
Copy link

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

@BlackGlory
Copy link

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
Copy link

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

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

9 participants