yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Yarn does not work in Git Bash on Windows

Open Yagun opened this issue 6 years ago • 65 comments

Windows 7 Yarn 1.3.2 (the same issue with 1.1.0) Git 2.16.1.windows.4

The path to Yarn is in the Environment PATH variable: D:\Program Files (x86)\Yarn\bin

Yarn works in Windows cmd.exe and PowerShell console. For example: c:>yarn --version

But does not work in Git Bash: $ yarn --version

module.js:538 throw err; ^

Error: Cannot find module 'C:\Program Files\Git\yarn.js' at Function.Module._resolveFilename (module.js:536:15) at Function.Module._load (module.js:466:25) at Function.Module.runMain (module.js:676:10) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3

I restarted Windows after yarn installation.

Yagun avatar Feb 10 '18 23:02 Yagun

What is the output of which yarn in Git Bash?

torifat avatar Feb 11 '18 07:02 torifat

/d/Program Files (x86)/Yarn/bin/yarn

Yagun avatar Feb 11 '18 09:02 Yagun

Just found that yarn.cmd command works.

For example: $ yarn.cmd --version 1.3.2

But: $ yarn --version module.js:538 throw err; ^ Error: Cannot find module 'C:\Program Files\Git\yarn.js'

Yagun avatar Feb 11 '18 10:02 Yagun

And, what is the output of which yarn.cmd?

torifat avatar Feb 11 '18 10:02 torifat

$ which yarn.cmd

which: no yarn.cmd in (/c/Users/USERNAME_HERE/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/USERNAME_HERE/bin:/d/Program Files/Python35/Scripts:/d/Program Files/Python35:/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64:

... (lots of paths here) ...

/d/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin:/d/Caffe/python:/cmd:/d/Program Files (x86)/Yarn/bin:/c/Users/USERNAME_HERE/AppData/Local/Yarn/bin:/c/Users/USERNAME_HERE/AppData/Roaming/npm:/c/Users/USERNAME_HERE/AppData/Local/Yarn/bin:/usr/bin/vendor_perl:/usr/bin/core_perl)

Yagun avatar Feb 11 '18 11:02 Yagun

This doesn't seem like a problem with Yarn. Try adding it to PATH in ~/.bash_profile.

torifat avatar Feb 11 '18 11:02 torifat

Well, other things like npm and nodejs work fine. Only yarn does not.

I added the path to yarn to PATH to the profile this way: PATH=$PATH':/d/Program Files (x86)/Yarn/bin'

The error message is still the same.

Yagun avatar Feb 11 '18 14:02 Yagun

The same problem has occurred in msys2. I run msys2 on Windows 8.1.

$ yarn
module.js:559
    throw err;
    ^

Error: Cannot find module 'C:\yarn.js'
    at Function.Module._resolveFilename (module.js:557:15)
    at Function.Module._load (module.js:484:25)
    at Function.Module.runMain (module.js:703:10)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:665:3

I edited the yarn script and confirmed it. It seems to be caused by the result of sed becoming empty character.

$ cat /c/Program\ Files\ \(x86\)/Yarn/bin/yarn
#!/bin/sh
echo "0: $0"
argv0=$(echo "$0" | sed -e 's,\\,/,g')
echo "argv0: $argv0"
basedir=$(dirname "$(readlink "$0" || echo "$argv0")")
echo "basedir: $basedir"
...
[result]
$ yarn
0: /c/Program Files (x86)/Yarn/bin/yarn
argv0:
basedir: .

I do not know why it will be empty. Tentatively, replacing sh with zsh worked fine.

$ cat /c/Program\ Files\ \(x86\)/Yarn/bin/yarn
#!/bin/zsh
...
$ yarn --version
1.3.2

imudak avatar Feb 27 '18 04:02 imudak

The cause was found out. This is caused by MacType. https://sourceforge.net/p/msys2/discussion/general/thread/aa858e3e/

Please uninstall MacType and try it.

imudak avatar Mar 13 '18 01:03 imudak

I do not have MacType on my PC. So, in my case Yarn issue is not related to MacType.

Yagun avatar Mar 13 '18 10:03 Yagun

Same issues replicated on a freshly installed Windows 10 Pro 64-bit machine:

Git for windows 2.16.2-64-bit (Also tried with 2.15.x) NVM for Windows 1.1.6 (Also tried with 1.1.5) Yarn 1.5.1 (Also tried with 1.3.2)

When run from cmd.exe:

C:\Users\echristensen>yarn --version
1.5.1

When run from Powershell:

PS C:\Users\echristensen> yarn --version
1.5.1

When run from Git Bash (MINGW64):

echristensen@ECHRISTENSEN-PC MINGW64 ~
$ yarn --version
module.js:478
    throw err;
    ^

Error: Cannot find module 'C:\Users\echristensen\yarn.js'
    at Function.Module._resolveFilename (module.js:476:15)
    at Function.Module._load (module.js:424:25)
    at Module.runMain (module.js:611:10)
    at run (bootstrap_node.js:387:7)
    at startup (bootstrap_node.js:153:9)
    at bootstrap_node.js:500:3

The funny thing is that it works if I run the js file directly or the cmd from Git Bash:

echristensen@ECHRISTENSEN-PC MINGW64 ~
$ yarn.js --version
1.5.1
echristensen@ECHRISTENSEN-PC MINGW64 ~
$ yarn.cmd --version
1.5.1

emilchristensen avatar Mar 13 '18 22:03 emilchristensen

Bonus information: Git for Windows:

echristensen@ECHRISTENSEN-PC MINGW64 ~
$ which yarn
/c/Program Files (x86)/Yarn/bin/yarn

echristensen@ECHRISTENSEN-PC MINGW64 ~
$ which yarn.js
/c/Program Files (x86)/Yarn/bin/yarn.js

echristensen@ECHRISTENSEN-PC MINGW64 ~
$ which yarn.cmd
which: no yarn.cmd in (/c/Users/echristensen/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/echristensen/bin:/c/Python27:/c/Python27/Scripts:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/Program Files/dotnet:/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/cmd:/c/Program Files (x86)/Yarn/bin:%NVM_HOME%:%NVM_SYMLINK%:/c/Users/echristensen/AppData/Local/Microsoft/WindowsApps:/c/Users/echristensen/AppData/Local/Yarn/bin:/c/Users/echristensen/AppData/Roaming/npm:/c/Users/echristensen/AppData/Roaming/nvm:/c/Program Files/nodejs:/usr/bin/vendor_perl:/usr/bin/core_perl)

cmd.exe:

C:\Users\echristensen>where yarn
C:\Program Files (x86)\Yarn\bin\yarn
C:\Program Files (x86)\Yarn\bin\yarn.cmd
C:\Program Files (x86)\Yarn\bin\yarn.js

emilchristensen avatar Mar 13 '18 22:03 emilchristensen

I have the very same issue here on Windows 10 Pro. It happened when Windows rebooted after some update installation.

Looks like this is a git bash environment or config problem, the yarn shell script could not set its basedir path correctly (looks like it's empty) and so tries to execute node yarn.js in current directory and of course there's no yarn.js to be found.

robotanz avatar Mar 16 '18 13:03 robotanz

Not sure if this would be a separate Windows Git Bash path issue but it fits the title here:

Windows 7 yarn v1.6.0 git version 2.7.2.windows.1

$ yarn
module.js:538
    throw err;
    ^

Error: Cannot find module 'C:\c\Program Files (x86)\Yarn\bin\yarn.js'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Resolved by adding MINGW64 to the yarn shell script basedir case statement:

case "$(uname -s)" in
  Linux) basedir=$(dirname "$(readlink -f "$0" || echo "$argv0")");;
  *CYGWIN* | *MINGW64*) basedir=`cygpath -w "$basedir"`;;
esac

jsweitzer avatar Apr 28 '18 12:04 jsweitzer

Same kind of patch works if you are using MSYS on Windows 10:

case "$(uname -s)" in
  Linux) basedir=$(dirname "$(readlink -f "$0" || echo "$argv0")");;
  *CYGWIN* | *MSYS*) basedir=`cygpath -w "$basedir"`;;
esac

ela34 avatar May 07 '18 20:05 ela34

I am getting the following error when trying to use yarn from git bash:

$ yarn --version module.js:538 throw err; ^

Error: Cannot find module 'C:\c\Program Files (x86)\Yarn\bin\yarn.js' at Function.Module._resolveFilename (module.js:536:15) at Function.Module._load (module.js:466:25) at Function.Module.runMain (module.js:676:10) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3

jdelucaa avatar May 23 '18 15:05 jdelucaa

I don't really know what i am doing, but i got a small and quick fix.

Edit C:\Program Files (x86)\Yarn\bin\yarn

Replace basedir=$(dirname "$(readlink "$0" || echo "$argv0")") by basedir=$( cd -P -- "$(dirname -- "$0")" && pwd -P )

This works for me. I hope i can help some people out.

Nielio avatar May 24 '18 08:05 Nielio

This might be a duplicate of https://github.com/git-for-windows/git/issues/1619. Is anyone here using Comodo Cloud Antivirus?

dahlbyk avatar Jun 04 '18 22:06 dahlbyk

@dahlbyk Not a duplicate, I'm afraid, as I am not using Comodo and have the same problem (identical situation so far to @Yagun's posts), except:

  • Windows 10 Pro
  • Yarn 1.6.0 (been happening since at least 1.3.0)
  • Git 2.17.1.windows.2

Any other information I can provide to help debug?

mordentware avatar Jun 05 '18 16:06 mordentware

@mordentware even if it's not specifically related to Comodo, this does seem like the same kind of problem. Are you able to reproduce these tests results: https://github.com/git-for-windows/git/issues/1619#issuecomment-394073051?

dahlbyk avatar Jun 05 '18 20:06 dahlbyk

Fair comment. Created the shell script below (copying here for benefit of future people):

#!/bin/sh
echo "A) $0"
echo "B) $0" | sed -e 's,\\,/,g'
echo "C) $(echo "$0" | sed -e 's,\\,/,g')"
echo "D) $(dirname "$(echo "$0" | sed -e 's,\\,/,g')")"
echo $(ls) | cat
echo $(ls | cat)
echo $(echo $(ls) | cat)

Output in Git Bash:

$ ./test.sh
A) ./test.sh
B) ./test.sh
C)
D) .
Files...
Files...
Files...

My sed-fu is weak, but I believe I should be expecting to see something in there? Based on the tweak to C:\Program Files (x86)\Yarn\bin\yarn suggested by @Nielio above, that was able to fix my issue.

I'm not confident enough to say exactly what the cause of the issue is, but happy to report that (for 1.6.0, at least) this seems to be very relevant.

mordentware avatar Jun 06 '18 19:06 mordentware

Looping in @dscho

dahlbyk avatar Jun 06 '18 21:06 dahlbyk

This looks like (don't laugh, it is really sad) the old Comodo Internet Security problem. See https://github.com/git-for-windows/git/issues/1619 for the full story.

dscho avatar Jun 09 '18 22:06 dscho

That was my thought, but @mordentware says he's not using Comodo. Not sure what else to try, or if there's anything here that's yarn-specific?

dahlbyk avatar Jun 10 '18 01:06 dahlbyk

Yeah I'm not using Comodo Internet Security either. I am however using Sophos Antivirus. But I've had it working on the same PC with the same software installed before a reinstall. After the fresh install of Windows 10 Pro with pretty much nothing on it but Sophos Antivirus, I got this error.

emilchristensen avatar Jun 10 '18 01:06 emilchristensen

@emilchristensen Is it possible you're using a different version of Node, which might be handled differently by Sophos? I don't suppose either of these products have any way of telling you when they so stuff that's affecting your runtime environment? 😒

dahlbyk avatar Jun 10 '18 04:06 dahlbyk

I've tried with multiple versions of node, including the same version as I ran before the reinstall. And yeah, I don't see any logs anywhere for whether Sophos is affecting my runtime.

emilchristensen avatar Jun 10 '18 07:06 emilchristensen

I have the exact same problem on Windows 7. Not using Comodo Internet Security. @Nielio's workaround works for me.

yonirab avatar Jun 10 '18 10:06 yonirab

Replace basedir=$(dirname "$(readlink "$0" || echo "$argv0")") by basedir=$( cd -P -- "$(dirname -- "$0")" && pwd -P )

@Nielio interesting!

Note: you will want to imitate the coding style by avoiding the extra spaces after/before the parentheses, and you definitely will want to quote the result (the original code gets this wrong, too).

Does it also work for you if you instead edit the case statement to catch *CYGWIN*|*MINGW*|*MSYS* instead of only *CYGWIN*? I think that would be the proper fix, and I guess that we will want to open a Pull Request in whatever project writes these shell scripts (it might be npm?)

dscho avatar Jun 21 '18 07:06 dscho

Does it also work for you if you instead edit the case statement to catch CYGWIN|MINGW|MSYS instead of only CYGWIN? I think that would be the proper fix, and I guess that we will want to open a Pull Request in whatever project writes these shell scripts (it might be npm?)

As can be seen here, I did exactly that.

dscho avatar Jun 21 '18 08:06 dscho