question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to include package.json in the patch

See original GitHub issue

I was commenting on here as we are (for some unknown reason so far) not able to download the latest beta version which seem to fix the issue about the error after applying a patch.

@ds300 let me explain out situation so maybe you can point the way to go. We are having this issue: a third party package we are using has not properly set the folder directories to point to the /lib and /es folders.

We opened this PR for them to merge but even if that happens then the other package we want to use rc-slider. So in total this means waiting on two external PRs.

As a workaround we wanted to modify or local packages, patch them, to allow this changes which already make our build succesful but we are not able to include the package.jsonfile. We tried yarn patch-package rc-util --use-yarn --exclude /$.^/ as a void regexp to reenable the disbales package.json tracking for the patch but with no success.

As another workaround we modified the /es files to patch the package but the patch-package is presenting the whitespace error This is usually caused by inconsistent whitespace in the patch file.

TL,DR: What would be the way to include package.jsonin the patch?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:13
  • Comments:21 (5 by maintainers)

github_iconTop GitHub Comments

69reactions
dawnmistcommented, Jun 19, 2020

The --include and --exclude options are there - but they’re used when creating patches for other packages, not during postinstall (where you’re applying patches to the various npm packages). You also need to be providing the --exclude option to override the default setting if you want to include changes to the package.json in the patch.

So to use it, you would first:

  • Modify the files inside the node_modules/package-to-be-patched so that they contained what you needed, then
  • Create the patch for the package using:
patch-package --exclude 'nothing' package-to-be-patched
  • Check that the patch file generated contains what you expected it to. If not, return to step 1 to update the contents of the package that you want to patch so that it reflects the correct information and redo the patch creation again.
  • Then set up postinstall so it applies the generated patches after each installation:
"postinstall": "patch-package && jetify"

You won’t be able to change the dependencies in another project’s package.json, because the patch is only applied after the dependencies have been installed. But you can do things like add a typescript type definition file to a project and update its package.json file to point to your new types.

14reactions
dawnmistcommented, Apr 24, 2018

If you can add an example for disabling the default exclude, that’d be fantastic.

The other time that being able to patch package.json is desirable is when adding typescript type definitions to a package that doesn’t have its own type definitions, so that you can add the “types” field to the package’s package.json file pointing to your new type definition file.

Edit: It doesn’t seem to matter what the exclude regex is, package.json is hardcoded to be excluded in the diff inside makePatch at: https://github.com/ds300/patch-package/blob/10c0a51d8f73fe67d414ef998a3eec6a001c14ba/src/makePatch.ts#L123-L126 and: https://github.com/ds300/patch-package/blob/10c0a51d8f73fe67d414ef998a3eec6a001c14ba/src/makePatch.ts#L135-L138

At present, the only way to be able to include package.json in the diff is to first patch patch-package to comment out those two lines in the compiled code. There’s a bit of humour there in using patch-package to patch patch-package itself…

Patch: patch-package+5.1.1.patch - to allow enabling patching of package.json if you also use a regex that doesn’t include it (e.g. --exclude '\*.txt').

patch-package
--- a/node_modules/patch-package/dist/makePatch.js
+++ b/node_modules/patch-package/dist/makePatch.js
@@ -66,14 +66,14 @@ function makePatch(packageName, appPath, packageManager, includePaths, excludePa
         fs.writeFileSync(path.join(tmpRepo.name, ".gitignore"), "!/node_modules\n\n");
         tmpExec_1("git", ["init"]);
         // don't commit package.json though
-        fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
+        // fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
         tmpExec_1("git", ["add", "-f", slash(path.join("node_modules", packageName))]);
         tmpExec_1("git", ["commit", "-m", "init"]);
         // replace package with user's version
         rimraf.sync(tmpRepoPackagePath);
         fsExtra.copySync(packagePath, tmpRepoPackagePath, { recursive: true });
         // remove package.json again
-        fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
+        // fs.unlinkSync(path.join(tmpRepo.name, "node_modules", packageName, "package.json"));
         // stage all files
         tmpExec_1("git", ["add", "-f", slash(path.join("node_modules", packageName))]);
         // unstage any ignored files so they don't show up in the diff
Read more comments on GitHub >

github_iconTop Results From Across the Web

React: Patching package.json using patch-package
The patch process comes after postinstall which means that the package.json is already processed with the invalid dependencies.
Read more >
The easiest way to patch your npm package when there is a
The easiest way and actually most devs do, is go to Github's package repository and log a bug.
Read more >
What Is package.json?
A package.json file must be valid JSON. This means any missing commas, unclosed quotes, or other formatting errors will prevent npm from interacting...
Read more >
package.json
package.json. The manifest file of a package. It contains all the package's metadata, including dependencies, title, author, et cetera.
Read more >
package.json
For this reason, make sure your plugin requirement is as broad as possible, and not to lock it down to specific patch versions....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found