Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
patch-package
Advanced tools
The patch-package npm package allows users to keep track of modifications to node_modules. It is useful when a user needs to make a quick fix to a node module and wants to persist this change across installs without waiting for the upstream fix. It lets you keep your workarounds in version control.
Creating Patches
This command is used to create a patch file for the npm package 'some-package'. After making changes to the node module, running this command will generate a patch file in a directory that can be committed to version control.
npx patch-package some-package
Applying Patches
This command applies all patches from the patches directory. It is typically used in the postinstall script in package.json to ensure that patches are applied after every npm install.
npx patch-package
Applying Specific Patch
This command applies a patch to a specific package. It is useful when you want to apply a patch to 'some-package' without applying all available patches.
npx patch-package some-package
Shrinkwrap is a tool for locking down the versions of npm packages installed. It is similar to patch-package in that it helps maintain consistency in dependencies, but it does not allow for modifications to the package code itself.
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.
In package.json
"scripts": {
+ "postinstall": "patch-package"
}
Then
npm i patch-package --save-dev
yarn add --dev patch-package postinstall-postinstall
To understand why yarn needs the postinstall-postinstall
package see: Why use postinstall-postinstall
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.
--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.
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.
Run patch-package
without arguments to apply all patches in your project.
--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.
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
Nope. The technique is quite robust. Here are some things to keep in mind though:
yarn
or npm
when switching between branches that do and don't have patch files.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
.
MIT
6.0.0-13
FAQs
Fix broken node modules with no fuss
The npm package patch-package receives a total of 1,150,808 weekly downloads. As such, patch-package popularity was classified as popular.
We found that patch-package demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.