Better handling for cached 'node_modules' folder
See original GitHub issueUse case
We are using patch-package
to fix some nasty dependency issues, it works great during development. But it sometimes causes CircleCI failures when a partially patched node_modules
folder is cached by CI.
The issue
- Install
patch-package
and add it asprepare
script, as instructed by README. - Patch module
foo-bar
usingpatch-package foo-bar
. Commit the resultedpatches/foo-bar+X.X.X.patch
to repo. - When CI is run, CircleCI installs and patches the
node_modules
folder and saves the result to cache. - Modify module
foo-bar
and regenerate patch file. - CircleCI restores the previous cached
node_modules
folder (which has incorporated previous version of the patch). - CircleCI fails when applying new patch to
node_modules
:
**ERROR** Failed to apply patch for package foo-bar
This error was caused because Git cannot apply the following patch file:
patches/foo-bar+X.X.X.patch
This is usually caused by inconsistent whitespace in the patch file.
Proposed solutions
-
Add a checksum file for the
patches
folder so we can easily check if patches has been changed before restoring cache from CI. -
Or, add a operation that can revert applied patches, such as
patch-package revert
. So we can revert the patches and save the cleannode_modules
folder to cache.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Speed up your CI and DX with node_modules/.cache
Your node_modules folder is very large and caching is significantly faster than running npm ci; You have a lot of postinstall build scripts, ......
Read more >How can I change the cache path for npm (or completely ...
You can change npm cache folder using the npm command line. ... Location of npmrc file: C:\Program Files\nodejs\node_modules\npm\npmrc prefix=D:\nodejs\npm ...
Read more >JavaScript Best Practices and node_modules - Julie Ng
A good PaaS caches for you Once it already exists, the install step runs significantly faster. So a good PaaS will handle any...
Read more >folders - npm Docs
Cycles, Conflicts, and Folder Parsimony. Cycles are handled using the property of node's module system that it walks up the directories looking for...
Read more >Caching in Node.js to optimize app performance
A cache layer generally provides a more efficient way of reusing previously retrieved or computed data. In this article, we'll cover the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi peeps. I just published v5.1.0 of patch-package which has a
--reverse
option. Thanks for your patience. 🙇I just ran into the exact same issue. Unfortunately I don’t think
--reverse
can help in our case—we don’t want to revert the patches because the version ofnode_modules
at the end of the build will be cached but also it is what will be used to run the application, so the patches need to be applied.I’m not sure there’s any solution to this problem, but at the very least I hope we can improve the error message, so it’s clear the patch failed because it’s being applied to something that has already been modified. The patch could carry a hash of the original file, and then when the patch is applied, if the hash of the target file doesn’t match the original, the patch would fail with an error along the lines of “target has unexpected hash”. /cc @ds300