Inquirer doesn't wait for input after prompt

70 Views Asked by At

For some reason, after prompting, inquirer does not wait for user input and directly moves to the next command line. Here's my code:


import inquirer from 'inquirer';

var link;
// const inquirer= require('inquirer');

inquirer
  .prompt([
    {message:"add link", name:"URL"}
  ])
  .then((answers) => {
    console.log(answers);
    // Use user feedback for... whatever!!
  })
  .catch((error) => {
    if (error.isTtyError) {
      // Prompt couldn't be rendered in the current environment
    } else {
      // Something else went wrong
    }
  });

import qr from 'qr-image'
import fs from 'fs'
var qr_svg = qr.image(link, { type: 'svg' });

fs.writeFile('user_input.txt',link,(err)=>{
    if(err){throw err;}
})

How do I fix this? This is happening even in the node terminal, and vs code. All my packages including node and npm are latest, even the modules are latest. What is causing the issue?

I was trying to generate a prompt, to which the user would reply with a link which would then be printed on screen. Instead, the prompt is generated, and instead of waiting for the input, the control jumps to the new command line.

1

There are 1 best solutions below

0
traktor On

I'll present a module to create an svg file of a QR code based on the posted code (I wouldn't have known where to start), with some modifications to actually create and save the qr-code asynchronously. Note I was unable to reproduce the problem presented in the post but hope this may provide assistance.

Setup

  1. Ensure Node and NPM are up to date and upgrade both as needed. If you get warning messages during installs about unsupported NPM or Node versions, come back to this step.

  2. Install packages in a (new) working directory of your choice.

     npm install inquirer
     npm install qr-image
    
  3. Create a create-qr.mjs file in the working directory with the following content:

    // create-qr.mjs
    
    import inquirer from "inquirer";
    import qr from "qr-image";
    import fs from "fs";
    import path from "path";
    
    inquirer
        .prompt([
            {message: "name of qr-code svg", name: "qrName"},
            {message: "URL for qr-code svg", name:"qrHref"}
        ])
        .then((answers) => {
            console.log("answers: ", answers);
            const {qrName, qrHref} = answers;
            const qr_svg = qr.imageSync(qrHref, { type: 'svg' });
            fs.writeFile( path.resolve( qrName+".svg"), qr_svg, (err)=>{
                if(err){throw err;}
            })
        })
        .catch((error) => {
            throw error;
        });
    
    

Test

Open a terminal in the working directory an run the module using

node create-qr.mjs

and enter both "stackoverflow" as the name of the qr code file and "https://www.stackoverflow.com" as the link's href value.

When the module exits (it's not coded to loop), open stackoverflow.svg in the working directory and use your smartphone to navigate to the link.


Notes

  • The code above is a simple stand-alone main module presented as an example of manually creating a qr-code: your requirements may be more complicated.

  • The svg name question is not expecting an extension, and always produces an svg file in code. Additional questions and validation could be useful if the code is to be distributed.

  • Creating and writing the qr code to device storage is moved into the promise chain so they are processed asynchronously after the questions have been asked.

  • The qr.image method of qr returns a readable stream, which fs.writeFile doesn't support. The code uses the qr.imageSync method as a quick fix because it (.imageSync) returns a buffer which fs.writeFile does support.

  • path.resolve resolves a relative path, such as a filename string, against the path of the current working directory.