patch-package
                                
                                
                                
                                    patch-package copied to clipboard
                            
                            
                            
                        patch-package 6.2.2 • Creating temporary folder SyntaxError: Unknown token: { line: 3, col: 2, type: 'INVALID', value: undefined } 3:2 in lockfile
Are you using Yarn 2?
Are you using Yarn 2?
I am.
and I'm also dealing with the same error here
patch-package does not yet work with yarn 2 for creating patch files but will work for patching on install. What I do is I use yarn 1 to create the patch file and then you can use yarn 2 normally.
patch-packagedoes not yet work with yarn 2 for creating patch files but will work for patching on install. What I do is I use yarn 1 to create the patch file and then you can use yarn 2 normally.
thanks!
is it possible to call yarn 1 from within a yarn 2 project?
No it's not. An "easy" way would be something like this:
mkdir tmp 
cd tmp 
yarn init -y
yarn add dep-name-goes-here
Make your changes inside node_modules.
npx patch-package dep-name-goes-here
And copy over the patch file.
No it's not. An "easy" way would be something like this:
mkdir tmp cd tmp yarn init -y yarn add dep-name-goes-hereMake your changes inside
node_modules.npx patch-package dep-name-goes-hereAnd copy over the patch file.
got it! thanks!
Are you using Yarn 2?
why?
Hi. Just faced this issue on yarn v3.0.2. Followed @gabrielbull solution to get it successfully working thanks! Can we expect a fix for this to support newer version of yarn as well?
@punndcoder28 I think you don't need patch-package with yarn v3. You can use yarn patch and yarn patch-commit to achieve same result.
First run yarn patch which will create temp folder where you can edit module. Then when you done run yarn patch-commit and yarn will create a patch for you.
⚠️ Also in my case I had to change patch version in package.json > resolution manually to match with dependency version. Here is an example repo
@hos's method works great, with one caveat: yarn doesn't seem to warn you if your patch is no longer being applied, or your "resolutions" are miss-configured. In these cases, it will silently ignore the patch and continue with regular installation.
Unlike patch-package which will loudly fail installation if your patch can't be applied.
This was enough to waste half a day for me, and force me to switch back to patch-package using @gabrielbull's method.
Edit: the specific failure case I ran into was using yarn berry + node_modules linker, then the package I patched had a new version released (minor), and so the resolutions stopped matching and the patch was silently ignored. But I didn't realise that for many frustrating hours. Whereas when the versions mismatch in patch-package, it fails loudly and immediately when installing deps.
Here's a script that automates @gabrielbull's method to the point it's the same as using patch-package with yarn v1.
mkdir tmp
cd tmp
npm init -y
npm install --save "$1"
rsync -av --exclude "node_modules" --delete "../node_modules/$1/" "./node_modules/$1/"
npx patch-package "$1"
mkdir -p ../patches
mv ./patches/* ../patches/
cd ..
rm -rf tmp
Save it as patch-package.sh to the root of your project and run chmod +x ./patch-package.sh (alternatively, call the script with sh)
Then you can run it like this:
./patch-package.sh dep-name-goes-here
This way you can test your changes of the dependency in the project you're using this dependency and then save the patch with a one-liner, just like before.
There's probably a way to save this script and alias it globally, but I am OK with how it turned out already, so I'll leave it for someone else
P.S. this solution might seem a bit crude compared to switching to yarn patch, but I agree with @jesstelford's reasoning for using patch-package instead, silent errors are the worst time wasters.
Also, for anyone wishing to use patch-package with yarn berry (v2+), here's a plugin that allows to run patch-package on postinstall
https://github.com/GravitywellUK/yarn-plugin-postinstall
I have the same issue with Yarn 3.2.0.
I ended up checking out from a commit in my project where I used yarn v1, updated patch there, stashed it, and then checked back where I was before and committed my patch there, popping it from stash.
Couldn't find a way to easily transfer my old patch-package patches to yarn v3 format.
I've just publish my PR #507 as @unts/patch-package, you can give it a try.