You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cordova Electron打包依赖缺失:event-pubsub模块找不到问题求助

问题描述

基于Cordova + Angular开发Electron应用,已在主项目的node_modules中安装event-pubsub库。自定义Cordova插件tcp-plugin中使用以下代码引入该库:

var Events = self.require('event-pubsub');

执行cordova plugin add tcp-plugin引入插件后,ng build和cordova build electron均能成功完成,但运行打包后的Electron应用时,出现错误:

Module not found: event-pubsub
解决方案

方案1:将依赖打包进插件

  • 进入tcp-plugin插件的根目录,安装event-pubsub作为插件自身的依赖:
    npm install event-pubsub --save
    
  • 修改插件中的引入代码,使用相对路径指向插件自身node_modules中的库(假设插件的业务js文件在src目录):
    var Events = self.require('../node_modules/event-pubsub');
    
  • 更新主项目中的插件:
    cordova plugin remove tcp-plugin
    cordova plugin add /本地路径/到/tcp-plugin
    
  • 重新构建并打包应用:
    ng build && cordova build electron
    

方案2:在Electron主进程全局暴露模块

  • 找到主项目的Electron主进程入口文件(通常为electron-main.js),添加以下代码:
    const Events = require('event-pubsub');
    global.Events = Events;
    
  • 修改tcp-plugin中的代码,直接使用全局变量:
    var Events = global.Events;
    
  • 重新构建打包应用即可。

方案3:通过Cordova配置指定要包含的模块

  • 在主项目的config.xml中添加Electron平台的配置,指定打包时要包含的模块:
    <platform name="electron">
      <preference name="ElectronIncludeModules" value="event-pubsub" />
    </platform>
    
  • 重新执行构建打包命令,Cordova会自动将指定模块纳入Electron的资源目录,插件即可正常找到该模块。

内容的提问来源于stack exchange,提问作者Muhammad Hammad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 03:07:40