Electron-vue: Cannot find relative main module using remote in renderer

Created on 28 Jun 2017  ยท  5Comments  ยท  Source: SimulatedGREG/electron-vue

Describe the issue / bug.

As per the Electron documentation of the remote module: "Modules specified by their relative path will resolve relative to the entrypoint of the main process." But when trying const foo = require('electron').remote.require('./foo'); in the renderer process it throws Error: Cannot find module './foo'. This might be related to the webpack config as discussed in #42.

How can I reproduce this problem?
project/
โ”œโ”€โ”€ main
โ”‚   โ”œโ”€โ”€ foo.js
โ”‚   โ””โ”€โ”€ index.js
โ””โ”€โ”€ renderer
    โ””โ”€โ”€ main.js
// some relative module: main/foo.js
module.exports = 'bar';
// renderer process: renderer/main.js
import Vue from 'vue';

const foo = require('electron').remote.require('./foo');
Tell me about your development environment.
  • Node version: 8.1.2
  • NPM version: 5.0.3
  • vue-cli version: 2.8.2
  • Operating System: macOS Sierra

Most helpful comment

Hi @SimulatedGREG!

I was on holidays but just came back and found what I believe to be a good solution by editing the webpack.main.config.js and manually adding every relative module as new entries.

let mainConfig = {
  entry: {
    main: path.join(__dirname, '../src/main/index.js'),
    foo: path.join(__dirname, '../src/main/foo.js')
  },
}

All 5 comments

This is expected behavior with the presence of webpack, as it will bundle all main process code (within the dependency tree) into one single file. So the remote.require methods will not work with the initial setup as foo.js will never make it to production.

It might be worth updating the webpack configuration to support this in the future, but for the time being this will be on the back burner.

Related: https://simulatedgreg.gitbooks.io/electron-vue/content/en/file-tree.html

Going to close for now, but will keep this in mind. Feel free to comment back any further questions.

Hi @SimulatedGREG!

I was on holidays but just came back and found what I believe to be a good solution by editing the webpack.main.config.js and manually adding every relative module as new entries.

let mainConfig = {
  entry: {
    main: path.join(__dirname, '../src/main/index.js'),
    foo: path.join(__dirname, '../src/main/foo.js')
  },
}

I recommend you to not use this approach if you want to enjoy all webpack features, especially HMR. Please see https://medium.com/@develar/electron-very-fast-developer-workflow-with-webpack-hmr-e2a2e23590ad

If you want to transfer data between renderer and main โ€” consider to use IPC. You can see HMR ready IPC in the https://github.com/electron-userland/electrify (will be published as separate module soon).

If you want share code between processes โ€” you can simply put code not in the src/main, but in the src.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xiaomizhou66 picture xiaomizhou66  ยท  3Comments

oscar-g picture oscar-g  ยท  3Comments

Oriol-GG picture Oriol-GG  ยท  3Comments

webrtcn picture webrtcn  ยท  3Comments

kinoli picture kinoli  ยท  3Comments