gpt4 book ai didi

npm - 如何修复 npm 的错误

转载 作者:行者123 更新时间:2023-12-03 08:11:40 25 4
gpt4 key购买 nike

您好,我希望可以依靠您的帮助,我正在尝试重新创建 WhatsApp 机器人应用程序,但收到此错误,我不知道如何继续或解决。

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();

出现的错误如下:

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

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Codigospython\whatsappbot-node\node_modules\ora\index.js from D:\Codigospython\whatsappbot-node\app.js not supported. Instead change therequire of index.js in D:\Codigos python\whatsappbot-node\app.js to adynamic 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

最佳答案

您收到此错误的原因是 ora is now a pure ESM module并且不再支持 CommonJS。

解决此问题的一种方法是将您的项目转换为 ESM。但是,出于某些原因,您可能不想这样做,这是完全公平的。

在这些情况下,我们只能使用 the last version that supported CommonJS这是:

npm install <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae5f8ebcabfa4bea4bb" rel="noreferrer noopener nofollow">[email protected]</a> 

关于npm - 如何修复 npm 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70647869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com