patch-package icon indicating copy to clipboard operation
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

Open yashirooooo opened this issue 5 years ago • 15 comments

yashirooooo avatar Oct 13 '20 04:10 yashirooooo

Are you using Yarn 2?

gabrielbull avatar Nov 19 '20 04:11 gabrielbull

Are you using Yarn 2?

I am.

and I'm also dealing with the same error here

AaronJPA avatar Nov 20 '20 09:11 AaronJPA

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.

gabrielbull avatar Nov 20 '20 15:11 gabrielbull

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.

thanks!

is it possible to call yarn 1 from within a yarn 2 project?

AaronJPA avatar Nov 20 '20 18:11 AaronJPA

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.

gabrielbull avatar Nov 21 '20 04:11 gabrielbull

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.

got it! thanks!

AaronJPA avatar Nov 23 '20 11:11 AaronJPA

Are you using Yarn 2?

why?

rxliuli avatar Aug 30 '21 02:08 rxliuli

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 avatar Oct 11 '21 06:10 punndcoder28

@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 avatar Oct 16 '21 08:10 hos

@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.

jesstelford avatar Jun 08 '22 06:06 jesstelford

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.

v4dkou avatar Jun 21 '22 05:06 v4dkou

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

v4dkou avatar Jun 21 '22 06:06 v4dkou

I have the same issue with Yarn 3.2.0.

landsman avatar Aug 22 '22 16:08 landsman

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.

pistonsky avatar Dec 20 '22 18:12 pistonsky

I've just publish my PR #507 as @unts/patch-package, you can give it a try.

JounQin avatar Jan 06 '24 18:01 JounQin