3

I basically want to do something like this:

$ my-node-cli <some-param> | less

Note that less is just an example. I need it to work with any other *nix command.

More about the use case:
I wrote a node CLI package that searches some online resource and outputs results to the shell. Since the result set can be huge, client wants to do additional operations on it, e.g grep, head, tail, tee, ... anything really.

I searched far and wide and I only managed to find the way to pipe into node program, not out of. My current idea is to capture the right side of pipe when my program is called, then, after I obtain results, execute my results concatenated with pipe (and that part I remembered when I was called) using child_process.exec. Not sure whether that could work though?

Note that each time my program is called it's a new process, i.e. the program doesn't have it's own prompt.

Thanks

1
  • 2
    What do you mean, it "outputs results to the shell"? You mean it simply writes to stdout? Then why wouldn't that pipe work? Commented Jun 4, 2015 at 19:20

1 Answer 1

3

All you need to do is output from STDOUT in your application. This will be sent to the next program if piped to it.

You can use plain old console.log() or the process.stdout stream.

It's up to the shell to handle stream redirection, not your application.

Sign up to request clarification or add additional context in comments.

1 Comment

Right, that was silly of me, that's why there are no resources on the web, it's so straightforward. Thanks guys, I had a bug which led me to believe that the pipe is what's causing it, when it was something inside my own code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.