ddiu8081 / blive-message-listener

Bilibili live danmu listener with type. 类型友好的 Bilibili 直播间弹幕监听库。
https://npmjs.com/blive-message-listener
MIT License
145 stars 13 forks source link

electron preload 中 exposeInMainWorld 使用该库报错 #3

Closed tymon42 closed 2 years ago

tymon42 commented 2 years ago

安装

yarn add blive-message-listener

引入错误:

Error: require() of ES Module C:\Users\why00\Documents\project\2someone-app\node_modules\blive-message-listener\dist\index.js from C:\Users\why00\Documents\project\2someone-app\dist\preload\index.js not supported.
Instead change the require of C:\Users\why00\Documents\project\2someone-app\node_modules\blive-message-listener\dist\index.js in C:\Users\why00\Documents\project\2someone-app\dist\preload\index.js to a dynamic import() which is available in all CommonJS modules.
    at __node_internal_captureLargerStackTrace (node:internal/errors:465:5)
    at new NodeError (node:internal/errors:372:5)
    at Module._extensions..js (node:internal/modules/cjs/loader:1148:19)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:829:12)
    at c._load (node:electron/js2c/asar_bundle:5:13343)
    at i._load (node:electron/js2c/renderer_init:33:356)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\why00\Documents\project\2someone-app\dist\preload\index.js:3:37)
    at Object.<anonymous> (C:\Users\why00\Documents\project\2someone-app\dist\preload\index.js:13:3)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1175:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:829:12)
    at c._load (node:electron/js2c/asar_bundle:5:13343)
    at i._load (node:electron/js2c/renderer_init:33:356)
    at Object.<anonymous> (node:electron/js2c/renderer_init:73:2296)
    at ./lib/renderer/init.ts (node:electron/js2c/renderer_init:73:2423)
    at __webpack_require__ (node:electron/js2c/renderer_init:1:170)
    at node:electron/js2c/renderer_init:1:1242
    at ___electron_webpack_init__ (node:electron/js2c/renderer_init:1:1310)
    at node:electron/js2c/renderer_init:141:194
    at NativeModule.compileForInternalLoader (node:internal/bootstrap/loaders:312:7)
    at NativeModule.compileForPublicLoader (node:internal/bootstrap/loaders:252:10)
    at loadNativeModule (node:internal/modules/cjs/helpers:49:9)
    at Module._load (node:internal/modules/cjs/loader:811:15)
    at c._load (node:electron/js2c/asar_bundle:5:13343)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

使用预加载脚本报错:

源代码:

// preload/index.ts
import { startListen } from 'blive-message-listener'
contextBridge.exposeInMainWorld(
  'biliApi',
  {
    startListen,
  },
)

// ****.vue
 window.biliApi.startListen(inputRoomId, handler)
console.log(typeof window.biliApi.startListen)     // undefine

报错:

TypeError: Cannot read properties of undefined (reading 'startListen')
    at DanmuTest.vue:31:5
    at callWithErrorHandling (runtime-core.esm-bundler.js:155:22)
    at callWithAsyncErrorHandling (runtime-core.esm-bundler.js:164:21)
    at hook.__weh.hook.__weh (runtime-core.esm-bundler.js:2684:29)
    at flushPostFlushCbs (runtime-core.esm-bundler.js:341:32)
    at render2 (runtime-core.esm-bundler.js:6234:9)
    at mount (runtime-core.esm-bundler.js:4424:25)
    at app.mount (runtime-dom.esm-bundler.js:1606:23)
    at main.ts:4:16

大佬能从报错看出问题吗?如果需要看源码请直接回复。

ddiu8081 commented 2 years ago

应该是没支持 cjs,v0.1.6 增加了 commonjs 导出,再试试呢

tymon42 commented 2 years ago

没报错了,弹幕数据获取正常了。
太酷了哥!