patch-package
patch-package copied to clipboard
support changing package.json
Hi!
I make some changes in package.json
file, add exports field
but patch-package
doesn't see any changes in it
does adding --exclude
at the end of the command make it work?
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.
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
#237 related
@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 you're welcome 😄
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...
is there a workaround for this?
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 itspackage.json
thennpm 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 themodule
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 : )
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
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.
Interestingly, the difficulty seems to have increased further, now I have to use this:
patch-package --exclude '^$'