Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug interactive node.js, such as Inquirer in VS Code

Tags:

node.js

I encountered a situation where node.js could not be debugged. The application entry file uses Interactive mode. It is with inquirer npm package.

I have used the debug tool of node,it doesn't work.

Here is the code:

const inquirer = require('inquirer');
const commonQuestions = [{...}]; // some code is omitted here
const shortCutQuestions = [{
  type: 'rawlist',
  name: 'cmd',
  message: '请选择快捷命令(直接输入数字进行选择):',
  pageSize: 10,
  default: 0,
  choices: cacheCommands,
}];

const shortCutResolveFunc = ({ cmd }) => {
  updateCommands(cmd);

  try {
    signale.watch(`cmd: ${cmd}`);

    const [globalCmd, ...cmdLineArgs] = cmd.split(' ');

    spawn.sync(globalCmd, cmdLineArgs, { stdio: 'inherit' });
  } catch (err) {
    console.log(pe.render(err));
  }
};

inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));

inquirer
  .prompt(isCommonMode ? commonQuestions : shortCutQuestions)
  .then(isCommonMode ? commonResolveFunc : shortCutResolveFunc);
like image 753
front_361 Avatar asked Apr 22 '26 16:04

front_361


1 Answers

Not sure what wasn't working, but was it perhaps that you hadn't configured your Visual Studio Code debugger to use a console other than the internalConsole (which is default).

here's an example of a launch.json configuration that uses the integratedTerminal:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Launch Program",
            "console": "integratedTerminal", // <= the relevant part
            "program": "${workspaceFolder}/main.js"
        }
    ]
}
like image 126
StephenWeiss Avatar answered Apr 24 '26 07:04

StephenWeiss