Skip to content

Prompts does not handle stdin redirection correctly #427

Description

@venomdev

Describe the bug

When running a script that uses prompts it can not accept a stdin redirection from a file. The file will be read but prompts ignores the new lines so that the whole file is used as the input to the first prompt only.

Steps to reproduce the behavior:

  1. Create a script that accepts multiple text inputs (prompts-test.js)
const prompts = require('prompts');

async function run() {
    const response = await prompts([
        {
            type: 'text',
            name: 'name',
            message: 'What is your project name?',
            initial: 'my-project',
        },
        {
            type: 'text',
            name: 'description',
            message: 'What is your project description?',
            initial: 'My project description',
        }
    ]);

    console.log('User entered:', response);
}

run().catch(e => {
    console.error(e);
    process.exit(1);
});
  1. Create a text file with answers to the prompt questions (project.txt)
My test project
A test project using prompts
  1. Run the script with redirection from the text file
node prompts-test.js < project.txt

Expected behavior

The script should accept each line from the text file as an answer to the single prompt and then show the response as an object on the console.

✔ What is your project name? … My test project
✔ What is your project description? … A test project using prompts
User entered: {
  name: 'My test project',
  description: 'A test project using prompts'
}

The actual result is that the script does not finish, does not show the console message, and does not trigger the catch exception function.

node prompts-test.js < project.txt
✔ What is your project name? … My test projectA test project using prompts


? What is your project description? › My project description%   

System

  • OS: Linux 6.8.0-53-generic x86_64
  • Terminal: zsh 5.9
  • Node version: v22.13.0

Additional context

The redirection to accept input from text files would be a great benefit to automate initializing projects using scripts and without waiting for user input.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions