Electron app starts to report an error after packaging: Cannot find module ‘reflect-metadata’



This is a series of pit-stepping journeys in the development process of Electron app. I hereby record them and share them with you. Each article is a pit and pit-filling solution!


Electron app starts to report an error after packaging: Cannot find module ‘reflect-metadata’

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)


According to the error message, it is expressed that the reflect-metadata module was not found in the packaged app.asar/node_modules, but I found this in the developed node_modules directory, but did not find reflect-metadata in app.asar/node_modules , so I suspect that reflect-metadata is not packaged into app.asar/node_modules when packaging

So guess if you need to package this dependency, does it require some additional configuration? According to this direction, I searched around the Internet and couldn’t find the relevant configuration instructions, so I submitted an issue on Github, and then I could only gnaw at the English documents on the official website!

Github issue link:https://github.com/nklayman/v…
Official document link:https://nklayman.github.io/vu…


I found a similar description in the official document, and passed the test according to the description. The official document description is as follows:

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']

The general idea is to tell developers that if you find that some modules do not work after packaging, you need to configure them in electronBuilder!


1、Vue CLI Plugin Electron Builder:https://nklayman.github.io/vu…