Vite 构建项目报错

发布于 2024-08-02  200 次阅读


直接集成SDK没问题,采用Vite构建的项目集成就会报下面的错误。

de:internal/modules/cjs/loader:1054 Uncaught Error: Cannot find module '../build/Release/agora_node_ext'
Require stack:
- E:\jinxiang\clientdesk\node_modules\agora-electron-sdk\js\Utils.js
- E:\jinxiang\clientdesk\node_modules\agora-electron-sdk\js\Private\internal\RtcEngineExInternal.js
- E:\jinxiang\clientdesk\node_modules\agora-electron-sdk\js\AgoraSdk.js
- electron/js2c/renderer_init
    at Module._resolveFilename (node:internal/modules/cjs/loader:1054:15)
    at o._resolveFilename (node:electron/js2c/renderer_init:2:3879)
    at Module._load (node:internal/modules/cjs/loader:900:27)
    at f._load (node:electron/js2c/asar_bundle:2:13330)
    at o._load (node:electron/js2c/renderer_init:2:3109)
    at Module.require (node:internal/modules/cjs/loader:1120:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (E:\jinxiang\clientdesk\node_modules\agora-electron-sdk\js\Utils.js:163:15)
    at Object.<anonymous> (E:\jinxiang\clientdesk\node_modules\agora-electron-sdk\js\Utils.js:175:3)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
M

SDK版本:4.2.6
Electron版本:24.6.2
用解决这一问题。vite-plugin-resolve
vite-plugin-resolve插件的具体使用方式,参考官方文档
可以参考我下面的代码:
下载:

npm i vite-plugin-resolve -D

新建js文件agora_introduce.js

插件配置示例代码:

import resolve from 'vite-plugin-resolve';
export const ConfigResolvePlugin = () => {
  return resolve({
    'agora-electron-sdk': `
      const { createAgoraRtcEngine } = require("agora-electron-sdk")
      export {
        createAgoraRtcEngine
      }
    `,
  });
};

配置好后,项目中正常导入插件使用就行,代码如下:

import { createAgoraRtcEngine } from 'agora-electron-sdk';