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.
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
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.
Install packages in a (new) working directory of your choice.
Create a
create-qr.mjsfile in the working directory with the following content:Test
Open a terminal in the working directory an run the module using
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.svgin 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.imagemethod ofqrreturns a readable stream, whichfs.writeFiledoesn't support. The code uses theqr.imageSyncmethod as a quick fix because it (.imageSync) returns a buffer whichfs.writeFiledoes support.path.resolveresolves a relative path, such as a filename string, against the path of the current working directory.