How to use the stdin function from process

Find comprehensive JavaScript process.stdin code examples handpicked from public code repositorys.

In Node.js, process.stdin is a readable stream that represents standard input (stdin) data that is coming into a running process from the command line or another source.

107
108
109
110
111
112
113
114
115
116
// This is used as a fallback, in case the standard method fails.
// With this in place, all tests pass under Node 13.7.
function readStdin() {
    let promise = new Promise((resolve, reject) => {
        var chunks = [];
        process.stdin
            .on("data", function(chunk) {
                chunks.push(chunk);
            })
            .on("end", function() {
fork icon2
star icon39
watch icon10

27
28
29
30
31
32
33
34
35
36
37
38
39
let ready = false




if (process.platform === "win32") {
    let rl = require("readline").createInterface({
        input: process.stdin
    })


    rl.on("SIGINT", function () {
        process.emit("SIGINT")
fork icon1
star icon5
watch icon0

How does process.stdin work?

In Node.js, process.stdin is a Readable stream that represents the standard input stream, typically used to read input from the command line or another source. The stdin stream is automatically created by Node.js when a process is started, and it can be accessed in the process object. To read input from process.stdin, you can listen for the 'data' event on the stream. When data is received, the event will be emitted, and you can read the input data from the stream using the read method or by consuming the stream with a Readable stream consumer. By default, process.stdin is in "paused" mode, which means that data is not read from the stream until the resume method is called on the stream. This is done to avoid consuming unnecessary resources when input is not being read. To start reading from process.stdin, you can call the resume method on the stream. It's important to note that process.stdin is a blocking synchronous stream by default, which means that the input will block the execution of the rest of your code until it has been fully read. To avoid blocking, you can use the setRawMode method to put the stream in raw mode, which enables asynchronous input handling. Overall, process.stdin provides a simple way to read input from the command line or another source in Node.js, and it can be a powerful tool for creating command-line applications and other types of interactive programs.

12
13
14
15
16
17
18
19
20
21
22
path.join(__dirname, '/data/Astrosat.json');
path.join(__dirname, '/data/BtoC.json');
path.join(__dirname, 'index.html');
path.join(__dirname, 'script.js');


// var standard_input = process.stdin;
// standard_input.setEncoding('utf-8');
// console.log("Specify data folder for json files or press enter for default: ");
// standard_input.on('data', function(data) {
//     if (data != "\n") {
fork icon4
star icon3
watch icon2

1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705


const command = argParser(process$1.argv.slice(2), {
    alias: mergeOptions.commandAliases,
    configuration: { 'camel-case-expansion': false }
});
if (command.help || (process$1.argv.length <= 2 && process$1.stdin.isTTY)) {
    console.log(`\n${help.replace('__VERSION__', rollup.version)}\n`);
}
else if (command.version) {
    console.log(`rollup v${rollup.version}`);
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
process.stdin.resume(); // start reading from stdin

process.stdin.on("data", (data) => {
  const input = data.toString().trim(); // convert input to string and remove newline
  console.log(`You entered: ${input}`); // print input back to the console
});

In this example, we start reading from process.stdin by calling the resume method on the stream. We then listen for the 'data' event on the stream, which is emitted whenever new input is available. When data is received, we convert it to a string and remove any newline characters using the toString and trim methods. We then print the input back out to the console using console.log. With this code, you can run the Node.js script in a terminal window, type in some input, and see it echoed back to you on the command line.

722
723
724
725
726
727
728
729
730
731
if (!cb) {   // no callback, do readSync
  const buffer = Buffer.alloc(1);
  fs.readSync(0, buffer, 0, 1);
  return buffer.toString('utf8');
}
const stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function (key) {
fork icon0
star icon0
watch icon1

64
65
66
67
68
69
70
71
72
73
  else if (key == '\u0003') { // ctrl-c
    process.exit()
  }
}

process.stdin.setRawMode(true)
process.stdin.resume()
process.stdin.on('data', onKeyPress)

nodeWatch(argv.watch, { recursive: true }).on('change', (change, path) => {
fork icon0
star icon0
watch icon1

+ 5 other calls in file

5
6
7
8
9
10
11
12
13
14
15
16
  process.stdout.write("\nprompt > ");
};


const bash = () => {
  process.stdout.write("prompt > ");
  process.stdin.on("data", (data) => {
    let args = data.toString().trim().split(" ");
    let cmd = args.shift();


    commands[cmd]
fork icon0
star icon0
watch icon0

20
21
22
23
24
25
26
27
28
29
    if (key && key.ctrl && key.name == 'p') {
    process.exit();
    }
});

process.stdin.setRawMode(true);
process.stdin.resume();

rl.on('line', async (line) => {
    if (line == 'exit') {
fork icon0
star icon0
watch icon0

+ 2 other calls in file