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

时间:2024-03-17 07:57:02

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