Inquirer on node.js
Asked Answered
M

2

5
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

Monoculture answered 30/6, 2022 at 7:43 Comment(0)
L
25

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');
Lattermost answered 30/6, 2022 at 7:58 Comment(0)
B
0

My two cents here, upgrading to yarn 3 worked for me!

Broadloom answered 30/8, 2023 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.