Hum4n01d
Hum4n01d

Reputation: 1370

Externals with webpack and electron

I’m using React with Webpack and Electron and I’m trying to require the remote from the renderer process. My code is being transpired by Babel + Webpack but this line of code:

import {remote} from 'electron'

is giving me

Uncaught Error: Cannot find module "electron"

How can I fix this? I know that the issue is because electron injects the electron module on the rendering of the page which webpack doesn’t know about it.

const {remote} = window.require('electron')

Works fine because it’s ignored by webpack.

Upvotes: 1

Views: 1934

Answers (1)

Sanoop Jose
Sanoop Jose

Reputation: 74

If you are using a recent version of webpack, set the target property to electron-renderer in the renderer configuration.

module.exports = {
 target: 'electron-renderer',
 //... other configurations
}

If you are using webpack to bundle main(main.js), set the target property to electron-main in the main configuration.

module.exports = {
 target: 'electron-main',
 //... other configurations
}

Upvotes: 1

Related Questions