corepack
corepack copied to clipboard
Fails on Windows + Git BASH
The path to corepack binaries for package managers fail in an emulated Linux environment on Windows.
Below, you can see that it's using both the Windows physical "C:" drive path as well as the emulated Linux "/c/" path. The actual Windows path is: "C:\Program Files\nodejs\node_modules\corepack\dist\yarn.js" and the emulated Linux path is: "/c/Program\ Files/nodejs/node_modules/corepack/dist/yarn.js".
Git for Windows is a popular choice among developers and the Git BASH shell is often the default terminal for many VS Code developers.
It's not currently possible to use corepack in Git BASH and the error doesn't make it obvious what a developer should do unless they happen to know about the underlying pathing.
$ yarn --version
node:internal/modules/cjs/loader:1080
throw err;
^
Error: Cannot find module 'C:\c\Program Files\nodejs\node_modules\corepack\dist\yarn.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
at Module._load (node:internal/modules/cjs/loader:922:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Node.js v18.18.2
Do you understand what is causing this? Maybe something related to the which
package? I've never used Git BASH and don't have a Windows machine anyway, so I'm clueless on how to fix it; if you have a fix, please send a PR.
I've traced this issue to the file "C:\Program Files\nodejs\yarn".
Does anyone know if this file is created by corepack
, nvm
, or even yarn
itself? I couldn't find the original source code through a GitHub search.
Breaking file content
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/node_modules/corepack/dist/yarn.js" "$@"
else
exec node "$basedir/node_modules/corepack/dist/yarn.js" "$@"
fi
Fixed file content
Editing the original file with the content below results in successful yarn
commands.
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/node_modules/corepack/dist/yarn.js" "$@"
else
exec node "$basedir/node_modules/corepack/dist/yarn.js" "$@"
fi
If this file is owned / created by another project, we can close this issue and then create a new one for the other project.
It appears there was a patch made under the "MINGW-packages" repository on July 30, 2023.
However, running corepack enable yarn
replaces that manually edited local file with the unpatched version. I'm not certain where corepack
is getting copying the older, unpatched files from.
I am using git-bash on windows. Everything looks good to me.
admin@win10-admin MINGW64 /d
$ corepack enable
admin@win10-admin MINGW64 /d
$ yarn -v
1.22.21
admin@win10-admin MINGW64 /d
$ pnpm -v
8.14.0