Module Error using Inquirer in an OpenCLI Command

564 Views Asked by At

Using the example code here: https://oclif.io/docs/prompting I'm trying to implement a prompt in an OpenCLI command using Inquirer.

When I load the module as described in the example on that page:

import * as inquirer from 'inquirer';

and run the following code:

// @ts-ignore
let responses: any = await inquirer.prompt([{
   name: 'stage',
   message: 'select a stage',
   type: 'list',
   choices: [{name: 'development'}, {name: 'staging'}, {name: 'production'}],
}])
console.log(responses.stage);

I get the following error:

(node:9260) [ERR_REQUIRE_ESM] Error Plugin: j32md [ERR_REQUIRE_ESM]: require() of ES Module D:\dev\node\myapp\node_modules\inquirer\lib\inquirer.js from D:\dev\node\myapp\dist\commands\clear.js not supported.
Instead change the require of inquirer.js in D:\dev\node\myapp\dist\commands\clear.js to a dynamic import() which is available in all CommonJS modules.
module: @oclif/[email protected]
task: toCached
plugin: j32md
root: D:\dev\node\joomla3-2-markdown
See more details with DEBUG=*
(Use `node --trace-warnings ...` to show where the warning was created)
 »   Error: command clear:putput not found

I've tired different ways for loading the module and I get different errors all related to how I load the module. For example:

import inquirer from 'inquirer';

or

const inquirer = require('inquirer');

or

import inquirer = require('inquirer');

Can someone please help me understand what I need to do to get this to work?

Its a new openCLI project created with the CLI. I'm running node 18.12.1 on Windows 10 - all patched to the lates version.

1

There are 1 best solutions below

0
On BEST ANSWER

The default installation is Pure ESM. To install the older version use:

npm install --save inquirer@^8.0.0

and use

const inquirer = require('inquirer');

See the readme file in the module folder for more info.