From 2ea2c81f52b74b96f65ba4e2a08e404d31d5205b Mon Sep 17 00:00:00 2001 From: Thibaut SABOT <9283289+thibautsabot@users.noreply.github.com> Date: Sat, 5 Feb 2022 21:16:25 +0100 Subject: [PATCH] Don't use yarn if a package-lock.json file is found (#31926) Fixes https://github.com/vercel/next.js/issues/31755 --- packages/next/lib/helpers/should-use-yarn.ts | 14 +++++++++++--- packages/next/lib/install-dependencies.ts | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/next/lib/helpers/should-use-yarn.ts b/packages/next/lib/helpers/should-use-yarn.ts index c305361517f0b2e..27c15b1dbac7da2 100644 --- a/packages/next/lib/helpers/should-use-yarn.ts +++ b/packages/next/lib/helpers/should-use-yarn.ts @@ -1,13 +1,21 @@ import { execSync } from 'child_process' +import fs from 'fs' +import path from 'path' -export function shouldUseYarn(): boolean { +export function shouldUseYarn(baseDir: string): boolean { try { const userAgent = process.env.npm_config_user_agent if (userAgent) { return Boolean(userAgent && userAgent.startsWith('yarn')) + } else { + if (fs.existsSync(path.join(baseDir, 'yarn.lock'))) { + return true + } else if (fs.existsSync(path.join(baseDir, 'package-lock.json'))) { + return false + } + execSync('yarnpkg --version', { stdio: 'ignore' }) + return true } - execSync('yarnpkg --version', { stdio: 'ignore' }) - return true } catch (e) { return false } diff --git a/packages/next/lib/install-dependencies.ts b/packages/next/lib/install-dependencies.ts index 4c0399b0068890e..77987f0ddf7bd90 100644 --- a/packages/next/lib/install-dependencies.ts +++ b/packages/next/lib/install-dependencies.ts @@ -15,7 +15,7 @@ export async function installDependencies( deps: any, dev: boolean = false ) { - const useYarn = shouldUseYarn() + const useYarn = shouldUseYarn(baseDir) const isOnline = !useYarn || (await getOnline()) if (deps.length) {