All Projects â†’ ds300 â†’ Patch Package

ds300 / Patch Package

Licence: mit
Fix broken node modules instantly ðŸƒðŸ―â€â™€ïļðŸ’Ļ

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to Patch Package

flatn
flat node dependencies
Stars: ✭ 18 (-99.7%)
Mutual labels:  package-manager, dependency-manager, node-modules
Cpm.cmake
ðŸ“Ķ CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.
Stars: ✭ 560 (-90.76%)
Mutual labels:  dependencies, package-manager, dependency-manager
Sampctl
The Swiss Army Knife of SA:MP - vital tools for any server owner or library maintainer.
Stars: ✭ 149 (-97.54%)
Mutual labels:  package-manager, dependency-manager, package-management
upgreat
CLI for a painless way to upgrade your package.json dependencies!
Stars: ✭ 47 (-99.22%)
Mutual labels:  dependency-manager, dependencies, package-management
Renovate
Universal dependency update tool that fits into your workflows.
Stars: ✭ 6,700 (+10.52%)
Mutual labels:  dependencies, dependency-manager, package-management
Athens
A Go module datastore and proxy
Stars: ✭ 3,736 (-38.37%)
Mutual labels:  dependencies, package-manager, dependency-manager
nodecmsguide
Your guide to Node.js content management systems
Stars: ✭ 25 (-99.59%)
Mutual labels:  node-js, javascript-tools
basalt
The rock-solid Bash package manager
Stars: ✭ 16 (-99.74%)
Mutual labels:  package-manager, package-management
Diffson
A scala diff/patch library for Json
Stars: ✭ 258 (-95.74%)
Mutual labels:  patch, diff
Conan Center Index
Recipes for the ConanCenter repository
Stars: ✭ 310 (-94.89%)
Mutual labels:  package-manager, package-management
cpm
ðŸŒĩ A wrapper for package managers to make them consistent for those of us who are lazy.
Stars: ✭ 52 (-99.14%)
Mutual labels:  package-manager, package-management
Editscript
A library designed to diff and patch Clojure data structures
Stars: ✭ 281 (-95.36%)
Mutual labels:  patch, diff
Gsync
gSync is an rsync based library for sending delta updates of files to a remote server.
Stars: ✭ 344 (-94.33%)
Mutual labels:  patch, diff
merge-confidence
The home of WhiteSource's Merge Confidence feature, for Renovate and WhiteSource Remediate
Stars: ✭ 41 (-99.32%)
Mutual labels:  dependency-manager, dependencies
Hdiffpatch
a C\C++ library and command-line tools for Diff & Patch between binary files or directories(folder); cross-platform; run fast; create small delta/differential; support large files and limit memory requires when diff & patch.
Stars: ✭ 459 (-92.43%)
Mutual labels:  patch, diff
tmux-eaw-fix
tmux 2.6 äŧĨ降ãŦおいãĶ East Asian Ambiguous Character をå…Ļč§’æ–‡å­—ãŪåđ…でčĄĻįĪšã™ã‚‹
Stars: ✭ 16 (-99.74%)
Mutual labels:  diff, patch
Poet
Poet helps you declare, manage and install dependencies of Python projects, ensuring you have the right stack everywhere.
Stars: ✭ 312 (-94.85%)
Mutual labels:  package-manager, dependency-manager
Cget
C++ package retrieval
Stars: ✭ 370 (-93.9%)
Mutual labels:  package-manager, dependency-manager
Jsondiffpatch
Diff & patch JavaScript objects
Stars: ✭ 3,951 (-34.82%)
Mutual labels:  patch, diff
Gojsondiff
Go JSON Diff
Stars: ✭ 371 (-93.88%)
Mutual labels:  patch, diff

patch-package

patch-package lets app authors instantly make and keep fixes to npm dependencies. It's a vital band-aid for those of us living on the bleeding edge.

# 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

# commit the patch file to share the fix with your team
git add patches/some-package+3.14.15.patch
git commit -m "fix brokenFile.js in some-package"

Patches created by patch-package are automatically and gracefully applied when you use npm(>=5) or yarn.

No more waiting around for pull requests to be merged and published. No more forking repos just to fix that one tiny thing preventing your app from working.

Set-up

In package.json

 "scripts": {
+  "postinstall": "patch-package"
 }

Then

npm

npm i patch-package

You can use --save-dev if you don't need to run npm in production, e.g. if you're making a web frontend.

yarn

yarn add patch-package postinstall-postinstall

You can use --dev if you don't need to run yarn in production, e.g. if you're making a web frontend.

To understand why yarn needs the postinstall-postinstall package see: Why use postinstall-postinstall

yarn workspaces

Same as for yarn ☝ïļ Note that if you want to patch un-hoisted packages you'll need to repeat the setup process for the child package. Also make sure you're in the child package directory when you run patch-package to generate the patch files.

Heroku

For patch-package to work on Heroku applications, you must specify NPM_CONFIG_PRODUCTION=false or YARN_PRODUCTION=false. See this issue for more details.

Docker and CI

  • If having errors about working directory ("cannot run in wd [...]") when building in Docker, you might need to adjust configuration in .npmrc. See #185.
  • In your Dockerfile, remember to copy over the patch files before running [npm|yarn] install
  • If you cache node_modules rather than running yarn install every time, make sure that the patches dir is included in your cache key somehow. Otherwise if you update a patch then the change may not be reflected on subsequent CI runs.

CircleCI

Create a hash of your patches before loading/saving your cache. If using a Linux machine, run md5sum patches/* > patches.hash. If running on a macOS machine, use md5 patches/* > patches.hash

- run:
    name: patch-package hash
    command: md5sum patches/* > patches.hash

Then, update your hash key to include a checksum of that file:

- restore_cache:
    key: app-node_modules-v1-{{ checksum "yarn.lock" }}-{{ checksum "patches.hash" }}

As well as the save_cache

- save_cache:
    key: app-node_modules-v1-{{ checksum "yarn.lock" }}-{{ checksum "patches.hash" }}
    paths:
      - ./node_modules

Usage

Making patches

First make changes to the files of a particular package in your node_modules folder, then run

yarn patch-package package-name

or use npx (included with npm > 5.2)

npx patch-package package-name

where package-name matches the name of the package you made changes to.

If this is the first time you've used patch-package, it will create a folder called patches in the root dir of your app. Inside will be a file called package-name+0.44.0.patch or something, which is a diff between normal old package-name and your fixed version. Commit this to share the fix with your team.

Options

  • --create-issue

    For packages whose source is hosted on GitHub this option opens a web browser with a draft issue based on your diff.

  • --use-yarn

    By default, patch-package checks whether you use npm or yarn based on which lockfile you have. If you have both, it uses npm by default. Set this option to override that default and always use yarn.

  • --exclude <regexp>

    Ignore paths matching the regexp when creating patch files. Paths are relative to the root dir of the package to be patched.

    Default value: package\\.json$

  • --include <regexp>

    Only consider paths matching the regexp when creating patch files. Paths are relative to the root dir of the package to be patched.

    Default value: .*

  • --case-sensitive-path-filtering

    Make regexps used in --include or --exclude filters case-sensitive.

  • --patch-dir

    Specify the name for the directory in which to put the patch files.

Nested packages

If you are trying to patch a package at, e.g. node_modules/package/node_modules/another-package you can just put a / between the package names:

npx patch-package package/another-package

It works with scoped packages too

npx patch-package @my/package/@my/other-package

Updating patches

Use exactly the same process as for making patches in the first place, i.e. make more changes, run patch-package, commit the changes to the patch file.

Applying patches

Run patch-package without arguments to apply all patches in your project.

Options

  • --error-on-fail

    Forces patch-package to exit with code 1 after failing.

    When running locally patch-package always exits with 0 by default. This happens even after failing to apply patches because otherwise yarn.lock and package.json might get out of sync with node_modules, which can be very confusing.

    --error-on-fail is switched on by default on CI.

    See https://github.com/ds300/patch-package/issues/86 for background.

  • --reverse

    Un-applies all patches.

    Note that this will fail if the patched files have changed since being patched. In that case, you'll probably need to re-install node_modules.

    This option was added to help people using CircleCI avoid an issue around caching and patch file updates but might be useful in other contexts too.

  • --patch-dir

    Specify the name for the directory in which the patch files are located

Notes

To apply patches individually, you may use git:

git apply --ignore-whitespace patches/package-name+0.44.2.patch

or patch in unixy environments:

patch -p1 -i patches/package-name+0.44.2.patch

Dev-only patches

If you deploy your package to production (e.g. your package is a server) then any patched devDependencies will not be present when patch-package runs in production. It will happily ignore those patch files if the package to be patched is listed directly in the devDependencies of your package.json. If it's a transitive dependency patch-package can't detect that it is safe to ignore and will throw an error. To fix this, mark patches for transitive dev dependencies as dev-only by renaming from, e.g.

package-name+0.44.0.patch

to

package-name+0.44.0.dev.patch

This will allow those patch files to be safely ignored when NODE_ENV=production.

Benefits of patching over forking

  • Sometimes forks need extra build steps, e.g. with react-native for Android. Forget that noise.
  • Get told in big red letters when the dependency changed and you need to check that your fix is still valid.
  • Keep your patches colocated with the code that depends on them.
  • Patches can be reviewed as part of your normal review process, forks probably can't

When to fork instead

  • The change is too consequential to be developed in situ.
  • The change would be useful to other people as-is.
  • You can afford to make a proper PR to upstream.

Isn't this dangerous?

Nope. The technique is quite robust. Here are some things to keep in mind though:

  • It's easy to forget to run yarn or npm when switching between branches that do and don't have patch files.
  • Long lived patches can be costly to maintain if they affect an area of code that is updated regularly and you want to update the package regularly too.
  • Big semantic changes can be hard to review. Keep them small and obvious or add plenty of comments.
  • Changes can also impact the behaviour of other untouched packages. It's normally obvious when this will happen, and often desired, but be careful nonetheless.

Why use postinstall-postinstall with Yarn?

Most times when you do a yarn, yarn add, yarn remove, or yarn install (which is the same as just yarn) Yarn will completely replace the contents of your node_modules with freshly unpackaged modules. patch-package uses the postinstall hook to modify these fresh modules, so that they behave well according to your will.

Yarn only runs the postinstall hook after yarn and yarn add, but not after yarn remove. The postinstall-postinstall package is used to make sure your postinstall hook gets executed even after a yarn remove.

License

MIT

Empowered by Futurice's open source sponsorship program

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].