Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yarn does not work in Git Bash on Windows #5349

Open
Yagun opened this issue Feb 10, 2018 · 65 comments
Open

Yarn does not work in Git Bash on Windows #5349

Yagun opened this issue Feb 10, 2018 · 65 comments
Assignees

Comments

@Yagun
Copy link

Yagun commented Feb 10, 2018

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.

@ghost ghost assigned torifat Feb 10, 2018
@ghost ghost added the triaged label Feb 10, 2018
@torifat
Copy link
Member

torifat commented Feb 11, 2018

What is the output of which yarn in Git Bash?

@Yagun
Copy link
Author

Yagun commented Feb 11, 2018

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

@Yagun
Copy link
Author

Yagun commented Feb 11, 2018

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'

@torifat
Copy link
Member

torifat commented Feb 11, 2018

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

@Yagun
Copy link
Author

Yagun commented Feb 11, 2018

$ 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)

@torifat
Copy link
Member

torifat commented Feb 11, 2018

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

@Yagun
Copy link
Author

Yagun commented Feb 11, 2018

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.

@imudak
Copy link

imudak commented Feb 27, 2018

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
Copy link

imudak commented Mar 13, 2018

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.

@Yagun
Copy link
Author

Yagun commented Mar 13, 2018

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

@emilchristensen
Copy link

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
Copy link

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

@robotanz
Copy link

robotanz commented Mar 16, 2018

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.

@jsweitzer
Copy link

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

@ela34
Copy link

ela34 commented May 7, 2018

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

@jdelucaa
Copy link

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

@Nielio
Copy link

Nielio commented May 24, 2018

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.

@dahlbyk
Copy link

dahlbyk commented Jun 4, 2018

This might be a duplicate of git-for-windows/git#1619. Is anyone here using Comodo Cloud Antivirus?

@mordentware
Copy link

@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?

@dahlbyk
Copy link

dahlbyk commented Jun 5, 2018

@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: git-for-windows/git#1619 (comment)?

@mordentware
Copy link

mordentware commented Jun 6, 2018

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.

@dahlbyk
Copy link

dahlbyk commented Jun 6, 2018

Looping in @dscho

@dscho
Copy link

dscho commented Jun 9, 2018

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

@dahlbyk
Copy link

dahlbyk commented Jun 10, 2018

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?

@emilchristensen
Copy link

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.

@dahlbyk
Copy link

dahlbyk commented Jun 10, 2018

@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? 😒

@emilchristensen
Copy link

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.

@dscho
Copy link

dscho commented Mar 22, 2019

How did you update git in Windows Bash?

I think what we're talking about is the Git Bash included in Git for Windows. You upgrade it by installing a newer version of Git for Windows, as downloaded from https://gitforwindows.org/.

@spencore
Copy link

updating git for windows definitely fixed this problem for me.

@afilp
Copy link

afilp commented May 11, 2019

I believe this is related to the same problem, yarn works OK when run from the Windows side but not OK when run from the Bash side (for the exaxt same project):

While in bash, I get an error:
image

Note that git for windows is different than the one in bash:
image

@dscho I upgraded "Git for Windows"

@ggomez2k
Copy link

I don't know how relevant is, but I'm experiencing some odd behavior, if I try to open a folder from within VSCode, yarn would not work at all. Though if I right click the same folder from the Windows Explorer and select open with VSCode, yarn would work perfectly fine.

@hosseinalipour
Copy link

hosseinalipour commented Jun 25, 2019

I have a similar problem and @Nielio fix didn't help, it also happens in the windows command line!
yarn run v1.17.2 $ node scripts/start.js /usr/bin/bash: Files\git\bin\bash.exe: No such file or directory ?[2K?[1G?[31merror?[39m Command failed with exit code 127. ?[2K?[1G?[94minfo?[39m Visit ?[1mhttps://yarnpkg.com/en/docs/cli/run?[22m for documentation about this command.

isaacs pushed a commit to npm/cmd-shim that referenced this issue Aug 12, 2019
It has been reported tons and tons of times that the generated shell
scripts simply fail in Git Bash of the Git for Windows project. The
reason is that the Cygwin-specific basedir workaround is also required
for MSYS2 (which is used internally by the Git Bash).

Let's be nice to everybody and include support for MSYS2.

As MSYS2 distinguishes between MSYS/MINGW modes, triggered by setting
the environment variable MSYSTEM to the values MSYS, MINGW32 and
MINGW64, and as those modes are reflected in the output of `uname -s`,
we need to look not only for MSYS but also for MINGW in the output of
`uname`.

This addresses yarnpkg/yarn#5349 among other
issues.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>

PR-URL: #30
Credit: @dscho
Close: #30
Reviewed-by: @isaacs
@mohkale
Copy link

mohkale commented Mar 21, 2020

This is still an issue. I just upgraded to bash 4.4.23 and I still have problems. All yarn commands don't produce any output (there's just a somewhat large gap from when I start the command to my prompt, such as if I had run clear). Also, running yarn completely hides my prompt, which makes the shell basically unusable. I have to keep restarting my shell everytime I accidentally run yarn. Could you please add an option to disable winpty? It's super annoying.

@tonyfrenzy
Copy link

Since yarn.cmd works correctly as expected, the step below works for me without any stress.

Update your list of aliases in the .bash_profile file at C:\Users\yourUsername\.bash_profile like so:

alias yarn='yarn.cmd'

Subsequently yarn can be used normally anywhere including from >_ git bash.

@JulioJu
Copy link

JulioJu commented Sep 24, 2020

It's caused by Trend Micro rprichard/winpty#95 . I've tested now by installing Trend Micro Internet Security on a new fresh Microsoft Windows 2004 VM, and I experience this problem.

  • Git Bash https://gitforwindows.org/ bash for Windows version 2.24.0.windows.2 and 2.28.0.windows.1
  • nodejs-lts v12.18.4 or nodejs version 14.11.0
  • => when we run node --version we have the same problem than with yarn --version

I encounter this problem also with MSYS2 3.1.7-340.x86_64 2020-08-27 11:03 UTC and Cygwin 3.1.7(0.340/5/3) 2020-08-22 17:48 when I try to execute alias node="winpty node.exe" && node --version .

We should probably make a PR to call directly node.exe .

On npm they do that https://github.com/npm/cli/blob/latest/bin/npm

See my PR #8355 . Note that with this PR, if there is a need of a REPL (like yarn add @angular/cli && yarn ng new test), the REPL step is skipped. But as for npm.

Note that on Bash windows when we type type node it returns

node is aliased to `winpty node.exe'

In fact, it is defined as it into /etc/profile.d/aliases.sh.

@dscho
Copy link

dscho commented Oct 23, 2020

It seems that this here ticket is addressed, as of npm/cmd-shim#30 (comment).

@Yagun would you mind closing the ticket, please?

@YX-XiaoBai please note that this here bug tracker concerns itself about bugs in Git for Windows, not Git on macOS. I am not quite certain how the original report in this ticket is related to your report, but I am fairly certain that you are talking about a very different problem.

@SohelIslamImran
Copy link

Same issue.
C:\Users\Sohel Islam Imran\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: command not found

image

@dahlbyk
Copy link

dahlbyk commented Apr 25, 2022

@SohelIslamImran what does which node show? This seems like yarn was able to execute (this issue, which can be closed), but it can't find node.

@SohelIslamImran
Copy link

@dahlbyk No! It is find node. But error with yarn only. Can't execute yarn commands.
image

@JulioJu
Copy link

JulioJu commented Apr 25, 2022

Please try

node.exe $(type -p yarn) .

@SohelIslamImran
Copy link

This issue has been open since 2018. Will this not be resolved? @torifat

@SohelIslamImran
Copy link

Adding *MINGW64* in these files resolved my problrm.

image
image
image

@SohelIslamImran
Copy link

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

Thank you @jsweitzer

@dscho
Copy link

dscho commented Apr 27, 2022

image

This is unnecessary, as the *MINGW64* case is already included in the *MINGW* case.

However, the last one, the change to node_modules/yarn/bin/yarn seems totally legit.

@dahlbyk
Copy link

dahlbyk commented Apr 28, 2022

image

However, the last one, the change to node_modules/yarn/bin/yarn seems totally legit.

Proposed as yarnpkg/berry#4409.

@djimenezdev
Copy link

So for some reason I tried using the change to node_modules/yarn/bin/yarn and when I restarted my terminal It still gave me the error it couldn't find the yarn module.

But remembered that yarn offers a way of configuring yarn via a msi installation! as seen below:
Screenshot 2022-05-27 125254

And when I attempted to use the yarn install command again in one of my projects for dependencies I needed to install, it worked again! And verified it worked with other key yarn commands as well. I know it says on the installation page it says it recommends to use npm install --global yarn to install. But the npm command was not installing some essential yarn config in the C:\Program Files (x86)\Yarn\bin\yarn and this is where for me it was saying it couldn't find the module yarn

But using the msi I went back and notice that the Yarn folder was there and contained the yarn script in the bin folder and it worked

@alex-bluetrain
Copy link

5 years, still not fixed? is there any workaround other than not using git bash ?

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

No branches or pull requests