Repository navigation
yarn init ignores global default version #589
Description
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.24.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.
Environment / versions
Ubuntu 24.04.1 LTS24.04.2 LTS
Node.js v23.6.0v23.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 -yLogs
$ 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
MikeMcC399 commentedon Feb 12, 2025
Perhaps this is a documentation error?
aduh95 commentedon Feb 12, 2025
No that feels like a bug, tracked in #202