Home > database >  Inquirer on node.js
Inquirer on node.js

Time:07-01

PS D:\Cursos\nodeudemy\aulas\2_FUNDAMENTOS\10_abstracao_input> node index.js
D:\Cursos\nodeudemy\aulas\2_FUNDAMENTOS\10_abstracao_input\index.js:1
const inquirer = require('inquirer')
                 ^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Cursos\nodeudemy\aulas\2_FUNDAMENTOS\10_abstracao_input\node_modules\inquirer\lib\inquirer.js from D:\Cursos\nodeudemy\aulas\2_FUNDAMENTOS\10_abstracao_input\index.js not supported.
Instead change the require of inquirer.js in D:\Cursos\nodeudemy\aulas\2_FUNDAMENTOS\10_abstracao_input\index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (D:\Cursos\nodeudemy\aulas\2_FUNDAMENTOS\10_abstracao_input\index.js:1:18) {
  code: 'ERR_REQUIRE_ESM'
}

It happens every time i try to run a node file w/ inquirer

CodePudding user response:

If you read the docs it explains you this problem and it provides a link on how to solve the issue.

Inquirer v9 and higher are native esm modules, this mean you cannot use the commonjs syntax require('inquirer') anymore. If you want to learn more about using native esm in Node, I'd recommend reading the following guide. Alternatively, you can rely on an older version until you're ready to upgrade your environment:

npm install --save inquirer@^8.0.0

This will then allow import inquirer with the commonjs require:

const inquirer = require('inquirer');
  • Related