Electron app打包后启动报错:Cannot find module ‘reflect-metadata‘

前言

这是一系列Electron app开发过程中的踩坑之旅,特此记录下来,分享给大家,每一篇都是一个坑和填坑解决方案!

问题

执行打包命令后,打出来的app启动就报错,错误信息如下:

A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'reflect-metadata'
Require stack:
- /Applications/aDemo.app/Contents/Resources/app.asar/node_modules/@nestjs/core/index.js
- /Applications/aDemo.app/Contents/Resources/app.asar/background.js
- 
at Module._resolveFilename (internal/modules/cjs/loader.js:887:15)
at Function.n._resolveFilename (electron/js2c/browser_init.js:257:1128)
at Module._load (internal/modules/cjs/loader.js:732:27)
at Function.f._load (electron/js2c/asar_bundle.js:5:12913)
at Module.require (internal/modules/cjs/loader.js:959:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/Applications/aDemo.app/Contents/Resources/app.asar/node_modules/@nestjs/core/index.js:11:1)
at Module._compile (internal/modules/cjs/loader.js:1078:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1108:10)
at Module.load (internal/modules/cjs/loader.js:935:32)

分析

按照错误提示,是表达在打包后的app.asar/node_modules中没有找到reflect-metadata这个模块,但是我在开发的node_modules目录中找到了这个,但是在app.asar/node_modules中确实没有找到reflect-metadata,所以我怀疑是打包时没有将reflect-metadata打包进app.asar/node_modules

所以猜测如果需要将这个依赖打包进去是不是需要一些额外的配置?按照这个方向在网上找了一圈都没有找到相关配置的说明,于是我在Github上提交了一个issue,然后只能一点点啃官网的英文文档了!

Github issue链接
官方文档链接

解决方案

在官方文档中找到了类似的说明,并且按照说明测试通过,官方文档说明片段如下:

Native modules are supported and should work without any configuration, assuming nodeIntegration is enabled. If you get errors, you may need to set the native dependency as an webpack external (opens new window). It should get found automatically, but it might not. To do this, use the externals option:

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      // List native deps here if they don't work
      externals: ['my-native-dep'],
      // If you are using Yarn Workspaces, you may have multiple node_modules folders
      // List them all here so that VCP Electron Builder can find them
      nodeModulesPath: ['../../node_modules', './node_modules']
    }
  }
}

大意就是告诉开发者,如果发现打包后某些模块不工作,需要你在electronBuilder中配置一下!

参考资料

Vue CLI Plugin Electron Builder

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

极客先生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值