228, так ладно хуй с ним с кучей строк, рофл в том что я везде серфил хабр стековерфлоу чатгпт, а он не работает сука
просто же var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("New user name:", function(user) { rl.question("New password:", function(password) { var newUser = new User(user, password); // do something... rl.close(); } }); Код var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("New user name:", function(user) { rl.question("New password:", function(password) { var newUser = new User(user, password); // do something... rl.close(); } });
ЮТУБ, не ну братан чатгпт мне такую же хуйню выдал, скорей всего во мне проблема, но синтаксис конечно для инпута еще тупее я не видел
Работает он достаточно интересно, но все же рекомендую воспользоваться Bun, Deno или готовой зависимостью, например @clack/prompts , там это реализовано куда проще Вот примеры кода: Bun/Deno const name = prompt('What is your name?'); console.log(`Hello, ${name}!`); JS const name = prompt('What is your name?'); console.log(`Hello, ${name}!`); Node.js import readline from 'node:readline/promises'; import { stdin, stdout } from 'node:process'; const rl = readline.createInterface({ input: stdin, output: stdout, }); const name = await rl.question('What is your name? '); console.log(`Hello, ${name}!`); JS import readline from 'node:readline/promises'; import { stdin, stdout } from 'node:process'; const rl = readline.createInterface({ input: stdin, output: stdout, }); const name = await rl.question('What is your name? '); console.log(`Hello, ${name}!`); Node.js 2 import { text } from '@clack/prompts'; const name = await text({ message: 'What is your name?', }); console.log(`Hello, ${name}!`); JS import { text } from '@clack/prompts'; const name = await text({ message: 'What is your name?', }); console.log(`Hello, ${name}!`);
Есть мощная библиотека inquirer, она поддерживает как инпут текста, вариантов ответа и так далее. const inquirer = require('inquirer'); // Массив вопросов (промптов) const questions = [ { type: 'input', name: 'name', message: 'Как вас зовут?', validate: function (value) { if (value.trim()) { return true; } return 'Пожалуйста, введите ваше имя.'; }, }, { type: 'list', name: 'color', message: 'Какой ваш любимый цвет?', choices: ['Красный', 'Синий', 'Зеленый', 'Желтый'], }, { type: 'confirm', name: 'confirm', message: 'Вы уверены в своем выборе?', default: true, }, ]; // Запуск промптов inquirer.prompt(questions).then((answers) => { console.log('Ваши ответы:', answers); }); JS const inquirer = require('inquirer'); // Массив вопросов (промптов) const questions = [ { type: 'input', name: 'name', message: 'Как вас зовут?', validate: function (value) { if (value.trim()) { return true; } return 'Пожалуйста, введите ваше имя.'; }, }, { type: 'list', name: 'color', message: 'Какой ваш любимый цвет?', choices: ['Красный', 'Синий', 'Зеленый', 'Желтый'], }, { type: 'confirm', name: 'confirm', message: 'Вы уверены в своем выборе?', default: true, }, ]; // Запуск промптов inquirer.prompt(questions).then((answers) => { console.log('Ваши ответы:', answers); });