AaronJonAlex's avatar

yarn.lock is causing npm run dev to fail

just did a new laravel install 5.7.19 and I couldn't compile my javascript. so I went to the laravel-mix website and it said to run 'rm package-lock.json yarn.lock'. So I looked inside the folder and it had a yarn.lock file. How was that created? ...well after deleting it, npm run dev, was able to compile.

Steps to reproduce error

$ laravel new my-app && cd my-app

$ npm install

$ npm run dev

source: https://laravel-mix.com/docs/4.0/troubleshooting

0 likes
8 replies
bobbybouwmann's avatar

You probably have yarn and npm installed. You should one of them for your project. So if you want to run npm run dev you need to use npm install. If you want to use yarn you can run yarn dev to compile and run yarn to download all dependencies.

Removing those files most of time fixes weird errors in general! Sometimes they don't have something to do with Laravel Mix

ohffs's avatar

@bobbybouwmann this hit me recently too in slightly different circumstances. If mix sees a yarn.lock file it will try and run yarn - even if it's not installed (as far as I can tell). It hit me doing a CI build after I'd been debugging npm issues (again) and had used yarn as a test - then inside the CI build it didn't have yarn and started to crash.

I think some part of doing a laravel new triggers a yarn.lock file to be created too - so I'd guess it's related to the way mix works. I meant to put an issue on the mix github but then started drinking & eating all the food on earth in the run up to xmas ;-)

Happy xmas btw! :-D

ejdelmonico's avatar

yarn.lock comes in the laravel package by default. I usually remove it right after laravel new someproject finishes and before running npm install. In a CI environment, I believe that the service looks for .lock files and depending on the service looks to run yarn or npm first.

D9705996's avatar
D9705996
Best Answer
Level 51

I think that the issue comes from this piece of code

if (File.exists('yarn.lock')) {
    return `yarn add ${dependencies} --dev --production=false`;
}

return `npm install ${dependencies} --save-dev --production=false`;

Look like if you need to pull in dependencies for mix and use npm but have a yarn.lock file it tries yarn before defaulting to npm. Not an expert but I would imagine this causes issues.

D9705996's avatar

The laravel installer downloads http://cabinet.laravel.com/latest.zip which has a yarn.lock and composer.lock files included in the archive. Might be worth a pr to include a --npm and --yarn arguments to delete the unneeded files from the installer.

Thoughts on how to handle a default? Leave both, favour npm?

AaronJonAlex's avatar

@D9705996 - BINGO !!!

That would throw an error since I don't have yarn installed in my mac. Don't know why it was working fine before.

I ran it again today and it failed. But look what I found:

Running: yarn add vue-template-compiler --dev --production=false
/bin/sh: yarn: command not found
/Users/aaronjonalex/Code/test/node_modules/webpack-cli/bin/cli.js:244
                throw err;
                ^
Error: Command failed: yarn add vue-template-compiler --dev --production=false
/bin/sh: yarn: command not found

Silly me for not reading thru ;)

so I installed yarn and laravel works fine out of the box now.

We need to create a Pull Request to catch that error.

AaronJonAlex's avatar

THE BUG HAS BEEN SQUASHED

It's working now, even if you don't have yarn installed on your computer.

npm run dev, works faster now :)

anyway, Happy New Year !!!

Please or to participate in this conversation.