28

Was looking at a tape + tap video and tried to get it to work.
OS: Windows 7 Git Bash Shell

node main.js | ./node_modules/.bin/tap-spec

stdout is not a tty.

main.js:

var test = require('tape');
var add = require('./add');

test('add: two numbers add correctly', function(t) {
var actual = add(1,2);
var expected = 3;
t.equal(actual, expected);
t.end();
});

add.js:

module.exports = function(a, b) {
return a + b;
};

winpty node main.js | ./node_modules/.bin/tap-spec doesn't fix the problem.

FreddyNoNose
  • 478
  • 1
  • 7
  • 13

6 Answers6

59

Just to add my case, I was facing similar issue. Neither solution of using winpty did help, thus I used different hint on using node.exe instead of node when running a script (from Git bash in my case).

not working:

node myscript.js < some-input.txt > some-output.txt

working:

node.exe myscript.js < some-input.txt > some-output.txt
Thomas Urban
  • 4,649
  • 26
  • 32
  • 3
    This worked for me on windows 10 using git-bash terminal. – specifically_user3827668 Jun 29 '20 at 04:19
  • 1
    Just appending ".exe" did the trick! Very interesting, huh? – Gilberto Albino Jul 25 '20 at 11:57
  • 10
    Anybody have a clue why `node.exe` works but `node` doesn't? – enorl76 Aug 07 '20 at 16:41
  • 14
    @enorl76 It took a moment to get this, but Git bash is establishing some aliases in its `/etc/profile.d/aliases.sh`. This includes node which is tested for existing prior to declaring `alias node="winpty node.exe"`. So, `node` is implicitly run through winpty while `node.exe` is not. – Thomas Urban Aug 07 '20 at 21:40
  • @ThomasUrban seems like it makes sense to undo the node alias then? Sorry I'm a noob Windows user and new to git bash, although I definitely see how much I've been missing with cmd as opposed to bash – enorl76 Aug 09 '20 at 17:00
  • The alias has been installed as part of Git Bash and so you should expect it to be restored each time you install another upgrade for that. Maybe it is possible to have another profile script in your home folder resetting that centrally declared alias. – Thomas Urban Aug 10 '20 at 18:27
  • Thanks. This fixed the issue for me with python in MSYS2 on Windows 10. Didn't work: `python foo.py >bar`. Did work: `python2.7.exe foo.py >bar` (yes, we're stuck in python 2.7 still, but finally transitioning to 3). For my own curiosity, and for anyone curious: I tried `which python` and got `/c/msys64/mingw64/bin/python`. I then tried `which python2.7.exe` and got `/c/msys64/mingw64/bin/python2.7.exe`. – Gary Fixler Jan 19 '22 at 18:20
  • @GaryFixler There is nothing curious about this for bash is a tool usually found in Linux operating system which doesn't care for filename extensions. In Linux, `python` and `python.exe` are different entries in filesystem and there is no implicit idea of looking up `python.exe` if `python` doesn't match any file. That's specific to Windows. So, in your case there might be two distinct files matching either name or one of them is a symbolic link to the other. – Thomas Urban Jan 19 '22 at 22:11
  • @ThomasUrban - yes (I've been all-Linux at home since 2006), but MSYS2 on Windows (which I have to use at work) works a bit differently. You can call Windows executables without the .exe. There's a `python.exe` in that folder, but no `python`, but I can still `ls -i python`, which lists the files as `python`, and gives the same inode as `ls -i python.exe`. I haven't dug in deeper to see why I had to include the .exe in this case, though. – Gary Fixler Mar 21 '22 at 03:21
  • you can remove the alias, node continues to work fine if you use the experimental windows console support thing – Erik Aronesty Jun 07 '23 at 21:29
6

Diagnose :

Theres nothing wrong with the code, I get the following output : (OS : ArchLinux)

  add: two numbers add correctly

    ✔ should be equal


  total:     1
  passing:   1
  duration:  14ms

Its probably a problem with Windows 7 Git Bash Shell

I read somewhere : sending output through a pipe is broken with Git Bash

To discard it run the following command :

node -p -e "Boolean(process.stdout.isTTY)"

For it to work you need the following output : true


Solution (for Windows):

$ node -p -e "Boolean(process.stdout.isTTY)"
false

Using the winpty tool, it creates a hidden console and marshals I/O between it and Cygwin/GitBashshell emulated pty :

$ winpty node -p -e "Boolean(process.stdout.isTTY)"
true

READ MORE : Node.js doesn't run as tty on windows / cygwin Issue#3006

Community
  • 1
  • 1
EMX
  • 6,066
  • 1
  • 25
  • 32
  • How did you run the winpty command that is different from my original post? It failed for me. What are the exact commands you are typing? Thanks – FreddyNoNose Aug 25 '17 at 23:25
  • 1
    @FreddyNoNose, like I state in the answer :the winpty command would be the compiled [winpty tool](https://github.com/rprichard/winpty) – EMX Aug 25 '17 at 23:34
  • 1
    @FreddyNoNose : I don't use Windows, but if you follow the answer you will get it to work with Git Bash Shell – EMX Aug 25 '17 at 23:37
  • Thanks but it appears the official position is that it is not supported. Ref: https://github.com/nodejs/node/issues/14100 – FreddyNoNose Aug 25 '17 at 23:48
  • 5
    The solution to fix it, is to use the `-Xallow-non-tty` argument for `winpty` . Here's an example that errors as "not a tty" when used on git-bash. It's fixed by running `winpty -Xallow-non-tty python --help | less` – ninMonkey Sep 30 '19 at 20:06
2

I had same problem on console "stdout is not a tty"

This because of console, while installing Git there is an option which is choosing default terminal. Install again Git and choose terminal as Windows console and after that it should be fine.

enter image description here

Alperzkn
  • 467
  • 2
  • 10
  • 25
  • 3
    Did not work for me, ended with another problem. What did work for me was using "Windows Powershell" which is embedded in Windows10. – Kim Steinhaug Nov 10 '19 at 00:13
2

If you use GitBash in Windows

In the file ./bash_profile you must add that :

alias docker='winpty -Xallow-non-tty -Xplain docker'
skyho
  • 1,438
  • 2
  • 20
  • 47
2

Just switch from git bash to cmd first

$ cmd
Code Whisperer
  • 22,959
  • 20
  • 67
  • 85
1

node generateData.js > db.json runs on Visual Studio Code's Terminal : bash

RVscript
  • 49
  • 3