0

I've a simple Node.js command line program that uses commander and inquirer for taking command line inputs. It then connects to MongoDB Atlas to carryout its operations in index.js file, and after connection to DB is closed, it just hangs there till process is broken with Ctrl-C. Even if I call an empty function with no connection to DB, it still does the same. Here is the code:

#!/usr/bin/env node

const program = require('commander');
const { prompt } = require('inquirer');
const {addProduct, findProduct, findOrder} = require('./index');

const questions = [
    {
        type: 'input',
        name: 'id',
        message: 'Product ID'
    }
];

program
    .version('1.0.0')
    .description('Test program to create records');

program
    .command('search')
    .alias('s')
    .description('Find a product by ProductID')
    .action(() => {
        prompt(questions). then((answers) => {
            findProduct(answers)
        })
    }); 

program.parse(process.argv);   

What am I doing wrong here?

Edit: I've tried both program.prase() and program.parseAsync() with the same result.

SJaka
  • 712
  • 13
  • 40
  • Might need to see what `findProduct` does – Phil Aug 11 '22 at 01:25
  • Not related to `findProduct`, if I take that out and just prints `Hello World`, it still hangs. – SJaka Aug 11 '22 at 01:41
  • What versions of `commander` and `inquirer` are you using? – Phil Aug 11 '22 at 01:47
  • No problem here using the latest `commander` and `inquirer` with code updated to use ES modules ~ https://codesandbox.io/s/node-js-forked-bvhqg5?file=/src/index.js – Phil Aug 11 '22 at 01:51
  • Version: "commander": "^9.4.0", "inquirer": "8.2.4" Node version: v16.15.1 – SJaka Aug 11 '22 at 01:57
  • 1
    You need to open a new terminal and run `node src/index.js s` – Phil Aug 11 '22 at 02:12
  • I can confirm that it runs in codesandbox, but not on my mac with M1 processor. – SJaka Aug 11 '22 at 02:16
  • 1
    Works fine for me on my Intel Mac. I literally ran `yarn init -y`, added `"type": "module"` to `package.json`, ran `yarn add commander inquirer`, put the contents from the sandbox link into `cmd.js`, ran `node cmd.js s`, was prompted for `Product ID`, entered anything and it said _"Hello, World!"_ and exited. – Phil Aug 11 '22 at 02:22
  • Thanks @Phil. I can confirm that creating a minimal program like this does run and ends gracefully. Your comment have helped divert my attention to elsewhere in the program. Much appreciated! – SJaka Aug 11 '22 at 03:06

0 Answers0