简体   繁体   中英

Node Script to console.log and return data

Here is a dummy script:

console.log('hello');
process.stdout.write(JSON.stringify({ foo: 'bar' }));

If I were to run node index.js | jq. node index.js | jq. this would cause a parse error because of the console.log . Is it possible to have a Node script that does console logs to the terminal, but then the data that is to be returned from the script (that can then be piped to other scripts) is separate? I want in the example above, {foo: 'bar'} to be piped only to jq but I still want to see the hello logged to terminal.

According to NodeJS docs , the console.log() outputs to stdout (used as input by the piped jq command).

You can replace the console.log call by console.error since it outputs to stderr instead, and will not be read by the jq command. (unless you redirect stderr to stdout)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM