node.jsinquirer

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?


Solution

  • 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');