Lpc_dark
Lpc_dark

Reputation: 2952

Nodejs Printing to Non standard output

node 5.4.1 windows 10

//program.js
console.log("hello world");

What i want to achieve is node program.js 3>file.txt

//file.txt
hello world

I have tried:

fs.createWriteStream(null,{fd:3})
//creates error bad file descriptor

fs.openSync(null,{fd:3})
//Another error


var tty = require("tty");
var out = new tty.WriteStream(3);
//Empty print

I don't know what else to try. Any advice.

What is the 3 in 3>file.

I don't even know the term to search using. I have used:

File Descriptor

Non Standard Output

Output 3

If my question doesn't define clearly what i want please comment so i can correct where i have went wrong.

I have attempted to define:

What i want.

What i have tried.

Where I have look.

Any advice will be much appreciated.

Upvotes: 0

Views: 563

Answers (1)

rsp
rsp

Reputation: 111466

This program should do what you need:

var fs = require('fs');
var stream = fs.createWriteStream(null, {fd: 3});
stream.write("OK\n");

If run without proper redirection, it gives the "EBADF: bad file descriptor" error:

$ node fd3.js
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: EBADF: bad file descriptor, write
    at Error (native)

When run with a redirection of FD 3, it prints OK:

$ node fd3.js 3>&1
OK

If it doesn't work on Windows then maybe it's a bug in Node or the fs module and you should report it or see if it was reported already:

You didn't actually show what errors do you get so it's really hard to answer your question in any more detail.

Update

As I wrote in the comments it would be very helpful to see what exact error message is printed to have some idea if it is a bug in Node or in the fs module or maybe a problem with bad invocation, or error in the program - in your question you show how you open the streams but you don't show how you actually attempt to print anything.

For example, I can run my example programs in those two ways:

node fd3.js 2>&1

or:

node fd3.js 3>&4

In both cases I can say that I get a "bad file descriptor" error but both of those errors are actually different errors, on different descriptors, reported by different subsystems during different steps in the invocation.

After reading here that it's a problem with Windows that may not even have a concept of file descriptors higher than 2, I've done some experiments by running this bat file on Windows 10:

@echo off
echo abc 1>&3

And it works just fine. When I invoke it as:

winfdtest 3>file.txt

I get abc written in file.txt. In fact, I don't even get a bad descriptor error when I don't redirect fd 3 to a file, in which case it gets printed to the console and no errors are shown. On the other hand when I run it as:

winfdtest 4>file.txt

the abc gets printed to the console and the file.txt is empty so it means that the correct descriptor gets redirected, and those that are not redirected get printed to the console.

My conclusion is that the same should work in Node and if it doesn't then it's a bug. Unfortunately no exact code and no exact error messages was shown in the question so it's hard to say what is going wrong exactly.

Upvotes: 1

Related Questions