electron app打包后启动报错: Cannot find module 'reflect-metadata'
Electron APP打包后启动报错:找不到模块'reflect-metadata'。在package.json中,reflect-metadata存在!我不知道这是不是个bug。我需要帮助,谢谢!
首先,运行electron-serve是正常的,但是运行electron-builder打包成功后,启动就会报错:
A JavaScript error occurred in the main processUncaught 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)
以下是关于builder的一些配置:
pluginOptions: { electronBuilder: {
builderOptions: {
"appId": "com.example.app",
"productName":"aDemo",
"copyright":"Copyright © 2019",
// "directories":{
// "output":"./dist"
// },
// files: ['dist_electron/**/*'],
"dmg": {
// "background": "res/background.jpg",
"window": {
"x": 100,
"y": 100,
}
},
},
electronBuilder: {
externals: ['reflect-metadata'],
nodeModulesPath: ['./node_modules']
}
}
},
回答:
题主自答
分析
按照错误提示,是表达在打包后的app.asar/node_modules中没有找到reflect-metadata这个模块,但是我在开发的node_modules目录中找到了这个,但是在app.asar/node_modules中确实没有找到reflect-metadata,所以我怀疑是打包时没有将reflect-metadata打包进app.asar/node_modules
所以猜测如果需要将这个依赖打包进去是不是需要一些额外的配置?按照这个方向在网上找了一圈都没有找到相关配置的说明,于是我在Github上提交了一个issue,然后只能一点点啃官网的英文文档了!
Github issue链接:https://github.com/nklayman/v...
官方文档链接:https://nklayman.github.io/vu...
解决方案
在官方文档中找到了类似的说明,并且按照说明测试通过,官方文档说明片段如下:
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.jsmodule.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中配置一下!
参考资料
1、Vue CLI Plugin Electron Builder
以上是 electron app打包后启动报错: Cannot find module 'reflect-metadata' 的全部内容, 来源链接: utcz.com/p/936678.html