Skip to content

yarn init ignores global default version #589

Closed as duplicate
Closed as duplicate

Description

@MikeMcC399

Current behavior

If an alternate version of Yarn Modern is installed through corepack install --global, for instance yarn@4.0.0, the command yarn init, in an empty project directory, attempts to install Yarn Modern 4.5.2 4.6.0.

Desired behavior

If an alternate version of Yarn Modern is installed through corepack install --global, the command yarn init, in an empty project directory, should use the globally installed version, as shown in ~/.cache/node/corepack/lastKnownGood.json.

Corepack documentation Upgrading the global versions specifically mentions yarn init as an example to override the default version.

Image

Environment / versions

Ubuntu 24.04.1 LTS 24.04.2 LTS
Node.js v23.6.0 v23.7.0
First reported on: corepack 0.30.0
Reproducible also on: corepack 0.31.0
Using n as Node.js manager

Steps to reproduce

corepack disable
rm -rf ~/.cache/node/corepack
corepack enable yarn
corepack install --global yarn@4.0.0
cat ~/.cache/node/corepack/lastKnownGood.json

mkdir yarn-init-test
cd yarn-init-test
yarn -v
yarn init -y

Logs

$ corepack disable
rm -rf ~/.cache/node/corepack
corepack enable yarn
corepack install --global yarn@4.0.0
cat ~/.cache/node/corepack/lastKnownGood.json

mkdir yarn-init-test
cd yarn-init-test
yarn -v
yarn init -y
Installing yarn@4.0.0...
{
  "yarn": "4.0.0"
}
4.0.0
! Corepack is about to download https://repo.yarnpkg.com/4.5.2/packages/yarnpkg-cli/bin/yarn.js
? Do you want to continue? [Y/n]

Debug logs

export DEBUG=corepack
$ yarn -v
  corepack LastKnownGood file would be located at /home/mike/.cache/node/corepack/lastKnownGood.json +0ms
  corepack Search for default version: Found yarn@4.0.0 in LastKnownGood file +2ms
  corepack Checking /home/mike/github/yarn-init-test/package.json +0ms
  corepack Checking /home/mike/github/package.json +0ms
  corepack Checking /home/mike/package.json +0ms
  corepack Checking /home/package.json +0ms
  corepack Checking /package.json +1ms
  corepack Falling back to yarn@4.0.0 as no project manifest were found +0ms
  corepack Reusing yarn@4.0.0 found in /home/mike/.cache/node/corepack/v1/yarn/4.0.0 +8ms
4.0.0
$ yarn init -y
  corepack LastKnownGood file would be located at /home/mike/.cache/node/corepack/lastKnownGood.json +0ms
  corepack Search for default version: Found yarn@4.0.0 in LastKnownGood file +2ms
  corepack Checking /home/mike/github/yarn-init-test/package.json +0ms
  corepack Checking /home/mike/github/package.json +0ms
  corepack Checking /home/mike/package.json +1ms
  corepack Checking /home/package.json +0ms
  corepack Checking /package.json +0ms
  corepack Falling back to yarn@4.5.2+sha224.c2e2e9ed3cdadd6ec250589b3393f71ae56d5ec297af11cec1eba3b4 as no project manifest were found +0ms
  corepack Installing yarn@4.5.2 from https://repo.yarnpkg.com/4.5.2/packages/yarnpkg-cli/bin/yarn.js +4ms
  corepack Downloading to /home/mike/.cache/node/corepack/v1/corepack-6201-fa8f55d.d40be +1ms
! Corepack is about to download https://repo.yarnpkg.com/4.5.2/packages/yarnpkg-cli/bin/yarn.js
? Do you want to continue? [Y/n] n
/home/mike/n/lib/node_modules/corepack/dist/lib/corepack.cjs:21635
        throw new UsageError(`Aborted by the user`);
              ^

UsageError: Aborted by the user
    at fetchUrlStream (/home/mike/n/lib/node_modules/corepack/dist/lib/corepack.cjs:21635:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async download (/home/mike/n/lib/node_modules/corepack/dist/lib/corepack.cjs:21762:18)
    at async installVersion (/home/mike/n/lib/node_modules/corepack/dist/lib/corepack.cjs:21854:55)
    at async Engine.ensurePackageManager (/home/mike/n/lib/node_modules/corepack/dist/lib/corepack.cjs:22316:32)
    at async Engine.executePackageManagerRequest (/home/mike/n/lib/node_modules/corepack/dist/lib/corepack.cjs:22416:25)
    at async Object.runMain (/home/mike/n/lib/node_modules/corepack/dist/lib/corepack.cjs:23102:5) {
  clipanion: { type: 'usage' }
}

Node.js v23.6.0

Note: the documentation could be made clearer that corepack use creates a package.json if it does not exist. It only mentions that corepack use updates an existing package.json.

Activity

  1. MikeMcC399 commented on Feb 12, 2025

    @MikeMcC399
    ContributorAuthor

    Perhaps this is a documentation error?

  2. aduh95 commented on Feb 12, 2025

    @aduh95
    Contributor

    Perhaps this is a documentation error?

    No that feels like a bug, tracked in #202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions