npm包停止了对 require 导入方式的支持,只允许使用import 导入方式,怎么解决

文章讲述了在Node.js项目中,如何将传统的`require()`方法替换为ESM(ECMAScriptModules)的`dynamicimport()`,以解决在`@mistralai/mistralai`模块导入时遇到的Error。开发者需要根据不同环境(本地或Azure)动态创建MistralClient实例并设置APIkey。
摘要由CSDN通过智能技术生成

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\source\m-yuying-node\node_modules\@mistralai\mistralai\src\client.js from D:\source\m-yuying-node\utils\mistralai\index.js not supported.
Instead change the require of client.js in D:\source\m-yuying-node\utils\mistralai\index.js to a dynamic import() which

const esm = require('esm')
let mistralClient

(async () => {
  let MistralClient = await import('@mistralai/mistralai');
  MistralClient = MistralClient.default
  console.log('MistralClient', MistralClient, esm)
  if (global.isLocal) {
    let apiKey = '1'
    mistralClient = new MistralClient(apiKey);
  } else if (global.isAzure) {
    let apiKey = 'xxx'
    mistralClient = new MistralClient(apiKey);
  }
})();


const getMistralClient = () => {
  return mistralClient
}

module.exports = {
  getMistralClient,
}

我开发的chatgpt网站:

https://chat.xutongbao.top

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值