twibiral / obsidian-execute-code

Obsidian Plugin to execute code in a note.
MIT License
1.14k stars 68 forks source link

How to use stdin in JS? #337

Closed cklaffer closed 6 months ago

cklaffer commented 6 months ago

Can anyone point me in the right direction, how to use stdin in JavaScript code blocks?

I don't seem to get anything require("readline") - related working. And only using process.stdin.readLine()

Thanks a lot!

twibiral commented 6 months ago

Try

// import readline module
const readline = require("readline");

// create interface for input and output
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

// create empty user input
let userInput = "";

// question user to enter name
rl.question("What is your name\n", function (string) {
  userInput = string;

  console.log("Your name is " + userInput);

  // close input stream
  rl.close();
});

from https://www.educative.io/answers/how-to-get-user-input-from-command-line-with-javascript

Works perfectly for me. Please reopen the issue if the problem persists