Unable to use ipcRenderer in EF + TS + React without nodeIntegration: true
See original GitHub issuePreflight Checklist
- I have read the contribution documentation for this project.
- I agree to follow the code of conduct that this project follows, as appropriate.
- I have searched the issue tracker for a bug that matches the one I want to file, without success.
Issue Details
- Electron Forge Version:
- 6.0.0-beta.54
- Electron Version:
- 11.2.3
- Operating System:
- macOS 10.15.7
Expected Behavior
I am using Electron Forge with TypeScript and React. I attempted to import "electron"
into my renderer.tsx
file because
I want to have a functionality that allows a button click event to communicate with the main process.
// renderer.tsx
class Button extends React.Component
{
constructor(props: unknown)
{
super(props);
this.onClick = this.onClick.bind(this);
}
onClick(): void
{
Electron.ipcRenderer.send("button");
}
render(): JSX.Element
{
return (<button onClick={this.onClick}>run</button>);
}
}
// index.ts
function button(): void
{
console.log("Button clicked");
}
Electron.ipcMain.on("button", button);
// package.json
"mainConfig": "./webpack.main.config.js",
"renderer": {
"config": "./webpack.renderer.config.js",
"entryPoints": [
{
"html": "./app/index.html",
"js": "./app/renderer.tsx",
"name": "main_window"
}
]
}
Actual Behavior
Electron starts the app, but the renderer process crashes due to failing to import Electron in the renderer.tsx
file. The error given in the console is
Uncaught ReferenceError: require is not defined
at Object.electron (external "electron":1)
at __webpack_require__ (bootstrap:789)
at fn (bootstrap:100)
at Object.<anonymous> (renderer.tsx:5)
at Object../app/renderer.tsx (renderer.tsx:38)
at __webpack_require__ (bootstrap:789)
at fn (bootstrap:100)
at Object.0 (module.js:22)
at __webpack_require__ (bootstrap:789)
at bootstrap:856
This issue can be fixed by setting nodeIntegration: true
when creating the window, however this is a security risk and is heavily discouraged.
To Reproduce
The repository here should be able to reproduce this issue.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Unable to use ipcRenderer in EF + TS + React without ...
This issue can be fixed by setting nodeIntegration: true when creating the window, however this is a security risk and is heavily discouraged....
Read more >Electron Forge - Can't use ipcRenderer in the renderer file
Found Solution. The solution is to use ipcRenderer in a preload script. preload.ts import { ipcRenderer } from "electron";. index.ts
Read more >Require Is Not Defined In A Electron-React-Webpack ...
existsSync is not a function | import { ipcRenderer } from 'electron', ... type Unable to use ipcRenderer in EF + TS +...
Read more >Electron and TypeScript: how to use ipcMain and ipcRenderer ...
I was using preload.ts to ensure an interface between Svelte and ... webPreferences: { nodeIntegration: false, contextIsolation: true, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Using preload option provided by
@electron-forge/plugin-webpack
could solve this problemIn
package.json
src/preload.js
main.js
src/app.jsx
The above method works with the React+Webpack template.
If you wish to use
ipcRenderer
whennodeIntegration
isfalse
, you’ll need to use preload scripts.FYI, this isn’t a issue that’s specific to Electron Forge. If you need more assistance, please use one of Electron’s community forums.