Skip to content

Error was reported after Electron APP was packed: Cannot find module 'reflect-metadata' #1618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
geekmister opened this issue Dec 15, 2021 · 3 comments

Comments

@geekmister
Copy link

Describe the bug
Error was reported after Electron APP was packed: Cannot find module 'reflect-metadata'.In package.json, the reflect-metadata exists! I don't know if it's a bug. I need help, thanks!

To Reproduce

  1. First, running electron-serve can be used normally;
  2. But, error was reported after electron app was packed: cannot find module 'reflect-metadata';

Expected behavior
The Electron App can be packed and run normally

Screenshots

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)

Environment (please complete the following information):

  • custom config for vcp-electron-builder:
module.exports = {
   chainWebpack: config => {
       config.resolve.extensions
           .add('ts')
           .add('tsx')
           .add('js')
           .add('json')
   },
   pluginOptions: {
       electronBuilder: {
           builderOptions: {
               "appId": "com.example.app",
               "productName":"aDemo",
               "copyright":"Copyright © 2019",
               // "directories":{
               //     "output":"./dist"
               // },
               // files: ['dist_electron/**/*'],
               "dmg": {
                   // "background": "res/background.png",
                   "window": {
                       "x": 100,
                       "y": 100,
                   }
               },
           },
           electronBuilder: {
               externals: ['reflect-metadata'],
               nodeModulesPath: ['./node_modules']
           }
       }
   },
   pages: {
       index: {
           entry: "./src/render/pages/index/index.js",
           template: "./src/render/pages/index/index.html",
           filename: "index.html",
           title: "uiautotest"
       },
       worker: {
           entry: "./src/render/pages/worker/worker.js",
           template: "./src/render/pages/worker/worker.html",
           filename: "worker.html",
           title: "worker"
       }
   },
   devServer: {
       proxy: {
           '/api': {
               target: 'http://localhost:8081',
               changeOrigin: true,
               ws: true,
               pathRewrite: {
                   '^/api': '/'
               }
           }
       }
   }
}
  • terminal output from running vue info:
 System:
   OS: macOS 11.6
   CPU: (4) x64 Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz
 Binaries:
   Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
   Yarn: 1.22.10 - ~/.nvm/versions/node/v14.16.1/bin/yarn
   npm: 6.14.13 - ~/.nvm/versions/node/v14.16.1/bin/npm
 Browsers:
   Chrome: 96.0.4664.93
   Edge: Not Found
   Firefox: Not Found
   Safari: 15.0
 npmPackages:
   @ant-design/icons-vue:  6.0.1 
   @vue/babel-helper-vue-jsx-merge-props:  1.2.1 
   @vue/babel-helper-vue-transform-on:  1.0.2 
   @vue/babel-plugin-jsx:  1.0.6 
   @vue/babel-plugin-transform-vue-jsx:  1.2.1 
   @vue/babel-preset-app:  4.5.13 
   @vue/babel-preset-jsx:  1.2.4 
   @vue/babel-sugar-composition-api-inject-h:  1.2.1 
   @vue/babel-sugar-composition-api-render-instance:  1.2.4 
   @vue/babel-sugar-functional-vue:  1.2.2 
   @vue/babel-sugar-inject-h:  1.2.2 
   @vue/babel-sugar-v-model:  1.2.3 
   @vue/babel-sugar-v-on:  1.2.3 
   @vue/cli-overlay:  4.5.13 
   @vue/cli-plugin-babel: ~4.5.0 => 4.5.13 
   @vue/cli-plugin-pwa: ~4.5.0 => 4.5.13 
   @vue/cli-plugin-router:  4.5.13 
   @vue/cli-plugin-typescript: ^4.5.13 => 4.5.13 
   @vue/cli-plugin-vuex:  4.5.13 
   @vue/cli-service: ~4.5.0 => 4.5.13 
   @vue/cli-shared-utils:  4.5.13 
   @vue/compiler-core:  3.1.5 
   @vue/compiler-dom:  3.1.5 
   @vue/compiler-sfc: ^3.0.0 => 3.1.5 
   @vue/compiler-ssr:  3.1.5 
   @vue/component-compiler-utils:  3.2.2 
   @vue/devtools-api:  6.0.0-beta.15 
   @vue/eslint-config-typescript: ^7.0.0 => 7.0.0 
   @vue/preload-webpack-plugin:  1.1.2 
   @vue/reactivity:  3.1.5 
   @vue/runtime-core:  3.1.5 
   @vue/runtime-dom:  3.1.5 
   @vue/shared:  3.1.5 
   @vue/web-component-wrapper:  1.3.0 
   ant-design-vue: ^2.2.6 => 2.2.6 
   typescript: ^4.3.5 => 4.4.3 (4.3.5)
   vue: ^3.0.0 => 3.1.5 
   vue-cli-plugin-electron-builder: ^2.1.1 => 2.1.1 
   vue-eslint-parser:  7.11.0 
   vue-hot-reload-api:  2.3.4 
   vue-loader:  15.9.8 (16.8.3)
   vue-property-decorator: ^9.1.2 => 9.1.2 
   vue-style-loader:  4.1.3 
   vue-template-es2015-compiler:  1.9.1 
   vue-types:  3.0.2 
   vuex: ^4.0.2 => 4.0.2 
 npmGlobalPackages:
   @vue/cli: 4.5.13

Additional context
none

@geekmister
Copy link
Author

By looking at the official documentation, I found a solution.The official documentation 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']
    }
  }
}

doc link

@davimatos
Copy link

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

Wow!

Thank you so much man! I had been trying to find a solution for this error for days.

@dongyawan
Copy link

what should I do in vite.config.js 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants