How to fix bug ora npm

Viewed 316

Hello I hope I can count on your help, I am trying to recreate a WhatsApp bot application but I am receiving this error and I do not know how to continue or solve.

const fs = require('fs')
const ora = require('ora')
const chalk = require('chalk')
const { Client } = require('whatsapp-web.js')
const qrcode = require('qrcode-terminal')

const SESSION_FILE_PATH = './session.json'
let client;
let sessionData;

const withSession = () => {
    //si existe se carga el archivo con las credenciales
    const spinner = ora(`Cargando ${chalk.yellow('Validando Session con Whatsapp...')}`);
    sessionData = require(SESSION_FILE_PATH);
    spinner.start();
    client = new Client({
        session:sessionData
    })

    client.on('ready',() => {
        console.log('Cliente esta corriendo!')
        spinner.stop();
    })
}

//Esta funcion genera el qrcode
const withOutSession =  () => {

    console.log('No Tenemos session guardada');
    client = new Client();
    client.on('qr', qr => {
        qrcode.generate(qr, { small: true });
    });
    
    client.on('authenticated', (session) => {
        //guardar credenciales de session para usar luego
        sessionData = session;
        fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) => {
            if (err) {
                console.log(err);
            }
        });
    });

    client.initialize();

}

//
(fs.existsSync(SESSION_FILE_PATH)) ? withSession() : withOutSession();

The error that appears is the following:

PS D:\Codigos python\whatsappbot-node> node app.js D:\Codigos python\whatsappbot-node\app.js:2 const ora = require('ora') ^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Codigos python\whatsappbot-node\node_modules\ora\index.js from D:\Codigos python\whatsappbot-node\app.js not supported. Instead change the require of index.js in D:\Codigos python\whatsappbot-node\app.js to a dynamic import() which is available in all CommonJS modules. at Object. (D:\Codigos python\whatsappbot-node\app.js:2:13) { code: 'ERR_REQUIRE_ESM' }

enter image description here

1 Answers

The reason you get this error is because ora is now a pure ESM module and no longer supports CommonJS.

One way to get around this is to convert your project to ESM. However, there might be reasons where you might not want to do this which is completely fair.

In those cases we are limited to use the last version that supported CommonJS which is:

npm install ora@5.4.1 
Related