Electron Cannot find module on production build


#1

I’m new in electron. I have an electron project that uses webpack, typeorm, and react.

This code below will works on development mode.

<div id="root"></div>
    <script>
      {
        const typeorm = require('typeorm')
        typeorm.createConnection()
        window.typeorm = typeorm
      }
    </script>

But after I build my electron app into an executable app (.exe for example) it throws an error on the console developer tools.

Uncaught Error: Cannot find module 'typeorm'

enter image description here

Is there any ways to expose typeorm module?


#2

I have a feeling that the problem is likely related with the way you installed the module or with webpack configuration.

If you installed typeorm with npm, it is possible that the module is not in the proper node-modules folder:
did you install it with npm using --save?

install typeorm --save

if that’s so typeorm should be listed as dependence in the package.json and it will be installed in a local way for your program; however If you installed it globally (without --save) it wont work. That is because electron uses its own node and it probably wont use your system’s global modules (using global modules would be a bad idea, because if they could access to them, that would mean that your executable would only work in computers that have already installed that package).

If that’s not the problem, take a look to your webpack configuration; I am not an expert on it, but I do know that some webpack configurations limit the things that will be packaged in the executable’s asar file, the node-modules dependencies and in the non-asar source files.