Jax-p
Jax-p

Reputation: 8593

Import Inquirer as module in Node 13

I'm having troubles with import of Inquirer using modules in Node 13.12.0. Any other import works well. As long as I've been using Node 12.x with require() it worked well.

My use-case of anything.mjs

import fs from "fs"; // works well
import inquirer from 'inquirer'; // undefined

So I've tried to import only one exported module

import {prompt} from 'inquirer'; // The requested module 'inquirer' does not provide an export named 'prompt'

also tried:

import * as inquirer from 'inquirer'; // [Module] { default: undefined }

I've also tried to require() but it is not defined in modules anymore.


How should I properly import Inquirer in Node 13.12.0 using modules?

Upvotes: 2

Views: 4774

Answers (3)

Urigo
Urigo

Reputation: 3185

inquirer just released v9.0 and migrated to ESM modules. So now this will simply work:

import inquirer from 'inquirer';

const response = await inquirer.prompt([
  {
    type: 'input',
    name: 'question',
    message: 'Want to answer?'
  }
]);

console.log(response.question);

Upvotes: 2

aspxsushil
aspxsushil

Reputation: 524

Using ES Modules and enquirer 2.3.6 I am using it this way. We could pass types to prompt object.

import enquirer from 'enquirer';

const enquirerObj =  new enquirer();

const response = await enquirerObj.prompt({
    type:'confirm',
    name: 'question',
    message: 'Want to answer?'
  });

 console.log(response);

Upvotes: 0

Ayush Gupta
Ayush Gupta

Reputation: 9305

According to the docs, you can use require in ESM in Node 13 as follows:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const inquirer = require('inquirer');

Upvotes: 2

Related Questions