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
Requiring electron outside of main.js causes a TypeError #7300
Comments
Can you include line 4 of |
I believe it is referring to index.js of electron, which is itself located in node-modules of the project. Here are the contents of this file:
The entire stack trace suggests a conflict with React.js: |
What happens if you run It looks like the |
@nukeop How are you launching your app. Your `start script should look like. "scripts": {
"start": "electron ."
} I have a feeling you are attempting to run your node main.js Which won't work |
Right after this error is thrown, I am able to run
I have a watchify instance running in the background as I'm developing which is continually updating my package. I defined it in scripts section of package.json like this:
Any advice on avoiding bundling |
@nukeop Electron supports require internally so you don't need to use browserify. As far as I am aware, I'd you want to use browserify, you have to exclude "electron". |
Interesting, could it be that watchify/browserify is ruining this? It has worked ok so far. Now I am not sure how to run the program without it. |
Literally just run electron . From your main app folder, you don't need to bundle anything when using Electron as it has a full node environment internally. I'm going to close this out as it is a Browserify issue |
That's what I've been doing all along, packing the program into a single .js file, which is included in a simple html file with:
And everything works, except when I use require. This is a problem with interaction between the two modules. If I do not pack the whole program into a bundle, I have no easy way of running it, as my main.js only starts electron and loads the html file that includes the bundle. |
@nukeop The renderer process inside Electron has access to a full node/commonjs environment as well, so you don't need to bundle anything.
I'm not sure I understand here, any script you load in your HTML file has a full commonjs environment and can therefore use |
For anyone encountering this problem in the future and reading this thread, using |
FWIW, I ran into the same issue trying to use electron in the renderer process with create-react-app that uses webpack in the backend instead of browserify. Edit: I figured out why :-) We want to Another way to tell webpack to ignore a global name is to use a comment like |
@nukeop - thanks for your last post; it helped me a lot. |
yep fixed my create react app / webpack issue.
to
@srinathh how are you exposing/loading your CRA app as renderer in your main? are you building first (and modifying the html static resource paths) |
Yes my workflow currently is to basically run
Additionally, I have I'd actually like to be able to do the If you'd like to seen an example of the setup - take a look at https://github.com/srinathh/snippetfu |
I'm not using Electron, but Node-Webkit (nw.js). |
@nukeop |
@nukeop I got same error, but it is solved window.require trick, thanks a lot! |
|
@steric85 are you using browserify, babel or webpack? you might need to transpile your code |
@Alxmerino I am using webpack. |
make sure you're compiling your code |
@steric85, I faced the
|
I'm using the above method for accessing the ipcRenderer from electron in my Angular app but Angular change detection is not working when I update an Observable using an ipcRenderer message handler. |
in Angular 2 I used to call |
I'm facing a very similar problem to @nukeop and @srinathh: I set up my Electron + React + Webpack project following this article guide, where the author at the end mentions the trick with var fs = require('fs')
var path = require('path')
var pathFile = path.join(__dirname, 'path.txt')
if (fs.existsSync(pathFile)) {
module.exports = path.join(__dirname, fs.readFileSync(pathFile, 'utf-8'))
} else {
throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again')
} I've tried deleting node_modules/electron and installing again. |
Thanks to @tumbledwyer for posting the link to a solution that works for me:
The solution from Randy Findley: First, install Rescripts.
Then, change the scripts tags in package.json from this...
to this
Now add a new file called
Finally add another new file called
Now you can use the |
What @ledleds pointed out worked for me, using CRA with typscript and Electron. Then thing is that I declared webPreferences like this:
so that seems to be overwriting/setting nodeIntegration to false, so setting it to true and relaunching the app solved the issue (you have to relaunch the app in order to load Electron window with that config) setting nodeIntegration to true like this
|
This was the only good solution I found in terms of scalability, I do not think anyone should rely on window.require |
Why would window.require not be scalable? |
For anyone who happens to be struggling with this or similar issues and is using Vue and vue-electron-builder, see here |
For anyone still struggling with this especially regarding quitting the electron application through a react button please read on. What helped for me was the following:
mainWindow = new BrowserWindow({
//...
webPreferences: {
nodeIntegration: true,
},
});
const { ipcMain } = require("electron");
ipcMain.on("close-me", (evt, arg) => {
app.quit();
});
const ipcRenderer = window.require("electron").ipcRenderer;
<Button label="close" onClick={(e) => ipcRenderer.send("close-me")} /> Feel free to comment and give feedback I am still new to electron. My configuration: "electron": "9.2.0",
"electron-builder": "22.8.0",
"electron-packager": "15.0.0", |
solved for me!! Thanks @nukeop `<script> export default { process() { ipcRenderer.send('processIPC', 'foi') } } |
Does anyone know how to set up the proper type in typescript? I have the following at the moment export const electron = window.require('electron').remote I would like something like export const electron = window.require('electron').remote as ElectronType so the IDE knows how to autocomplete the electron API. |
@timsamart it does works. Thank you, it saves me days of work. 🤣 |
After spending many hours trying literally everything above, I missed the part in case you use
|
I'm using electron-typescript-webpack-react and electron-forge. This solution #9920 (comment) works only I put the window.api in index.html:
But if I put this code in the renderer process: renderer.ts I get the error:
I tried also to add
and I get
|
One reason some people are still experiencing this problem is that |
Not even last comment helped me. Im trying to follow this article emphasis on simple in title. And then spend two days to resolve errors that should not be there. Its ether There are x solutions, and articles and all are different from another, including this topic. This is ridicules. Today is 16.03.2012, allow me to ask: Does anyone has a suggestion how to modify simple script from article to work? |
Just so you know, that article references
in the |
Thank you for the replay, I made rollback of everything to versions from article, still same thing:
It is reporting and if I remove window I get |
Thank you very much! That was finally solve my problem. |
For @ikiK-CRO and others, I created my own "starter kit" project with Electron/TypeScript/Svelte using the latest (as of this posting) versions of the libraries with https://github.com/pglezen/electron-typescript-svelte-starter |
After many hours of trying to make this work, I finally got it to work using the
in
In the client application:
|
I need an answer to very simple question: If I have eg. From clipboard docs https://www.electronjs.org/docs/latest/api/clipboard
It suggests I can use it in both processes. But is it 'conditional' on context isolation or not? Right now I cannot import anything from If importing anything from electron is not possible with I understand security consequences and I don't want to disable For several hours I'm trying to fix this crash, not even sure if it is fixable or exepcted |
You should be able to import any renderer API you want from within your This stackoverflow answer helped me get a grip on this situation. |
it is worked in electron and web |
Hello
I am using Electron with React.js and babelify, among other plugins (list at the bottom).
Attempting to use
require('electron')
, for example to gain access to electron'sBrowserWindow
, results in the console showing the following error:index.js:4 Uncaught TypeError: fs.readFileSync is not a function
I can, however, use
const electron = require('electron');
in main.js. For what it's worth, I am also using watchify to pack everything into a js bundle.Here is the complete list of dependencies in my package.json:
The text was updated successfully, but these errors were encountered: