Skip to content
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

TypeError: Cannot read property 'on' of undefined #30

Closed
ghost opened this issue Jan 30, 2016 · 52 comments
Closed

TypeError: Cannot read property 'on' of undefined #30

ghost opened this issue Jan 30, 2016 · 52 comments

Comments

@ghost
Copy link

ghost commented Jan 30, 2016

After

> git clone https://github.com/atom/electron-quick-start
> cd electron-quick-start
> npm install
> npm start

I get error:

> electron-quick-start@1.0.0 start /home/do/dev/electron-quick-start
> electron main.js

App threw an error when running [TypeError: Cannot read property 'on' of undefined]
A JavaScript error occurred in the main process
Uncaught Exception:
TypeError: Cannot read property 'on' of undefined
    at Object.<anonymous> (/home/do/dev/electron-quick-start/main.js:34:4)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Object.<anonymous> (/home/do/dev/electron-quick-start/node_modules/electron-prebuilt/dist/resources/default_app/main.js:258:23)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
@vertexportus
Copy link

Having the exact same problem..

Tried to use the downloaded .exe, update node, clean cache and re-install electron-prebuilt, and nothing..

I either get an error that module electron can't be found (with require('electron') ) or that app is undefined with require('electron-prebuilt')

@dmr
Copy link

dmr commented Feb 5, 2016

I have the same issue here.
Did you resolve the problem by now?

@ghost
Copy link
Author

ghost commented Feb 5, 2016

@dmr, no, i switched to nw.js.

@dmr
Copy link

dmr commented Feb 6, 2016

The issue is also discussed in the electron main repo:
electron/electron#3708

const electron = require('electron');
const app = require('app');
const BrowserWindow = require('browser-window')

Solved the issue for me :)

@dmr
Copy link

dmr commented Feb 6, 2016

Anyone interested in a pull request?
Not sure how stable the new api is.

@ghost
Copy link
Author

ghost commented Feb 6, 2016

@dmr, I think that working example better.

@dmr
Copy link

dmr commented Feb 6, 2016

I think the issue was/is my computer. I had to manually look for a node_modules/electron folder and delete is. Sorry for the noise.

@jlord
Copy link
Contributor

jlord commented Feb 7, 2016

@chezstov do you have a globally installed electron module?

@ibufu
Copy link

ibufu commented Feb 26, 2016

@jlord
I installed electron globally.But it still throw the same error.

@dmr
Copy link

dmr commented Feb 26, 2016

That is exactly the problem.
"electron" is not the correct package name!

Remove alle the electron installations and install electron-prebuilt, then it works.

@jlord
Copy link
Contributor

jlord commented Feb 26, 2016

That is exactly the problem.
"electron" is not the correct package name!

Yep, @dmr. 👍

If you have a globally installed module called electron (not associated with Electron.js) you'll have a conflict @ibufu. You'll need to remove it and use electron-prebuilt for Electron.js apps.

// cc @chezstov @vertexportus

@jlord
Copy link
Contributor

jlord commented Mar 10, 2016

Going to close this out, hopefully the last response helped others in this thread.

@jlord jlord closed this as completed Mar 10, 2016
@Pablodotnet
Copy link

@dmr I can't make it work with that, can you put here a copy of your main.js? It would be great.

@dmr
Copy link

dmr commented Oct 27, 2016

This issue is really old. Removing node_modules and reinstalling will probably fix the issue

@ghost
Copy link

ghost commented Nov 29, 2016

Removing node_modules and reinstalling didn't work for me. Any insights?

Here is what I've done:

  • Updating node/npm
  • Using electron-prebuilt instead of electron
  • Made sure electron was not installed globally
  • Deleted node_modules and re-ran npm install

Here is the error message:

app.on('ready', createWindow());
   ^


TypeError: Cannot read property 'on' of undefined
    at Object.<anonymous> (/home/harry/web/webcalc/app.js:17:4)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:402:7)
    at startup (bootstrap_node.js:157:9)
    at bootstrap_node.js:521:3

Node version: v7.2.0
NPM version: 4.0.2
Electron version: 1.4.10

System is GNU/Linux running Linux Mint 18 Cinnamon 64 Bit

@davidcoleman007
Copy link

Same for me. electron-prebuilt is not a fix. and .on is called on a null object.

@jawatech
Copy link

The name "electron-prebuilt" is changed (back?) to "electron" since mid-2016 by the development team.
While you reinstall electron, entries in package.json should be changed accordingly.
(electron-prebuild => electron, and version number , and so on)

@benjaminsuch
Copy link

The thing is, although I get this error, it's still working?! createWindow gets executed.

@boredwookie
Copy link

One potential cause of this problem can be tied back to executing the electron app from a terminal extension in the Atom editor. This answer on StackOverflow helped me get past the problem: http://stackoverflow.com/a/40664925/1076507. Using a system terminal instead of platformio-ide-terminal plugin inside the Atom editor addressed my problem.

@taoteg
Copy link

taoteg commented Jan 9, 2017

@boredwookie Thanks you! Made it all the way down here thinking I was borked and then found your working solution. Thank you for saving me many hours of frustrating troubleshooting. This is the issue, not sure what's up with the electron terminal extension not issuing this properly but whatever... small steps. =)

@codekraft-studio
Copy link

@boredwookie man you saved me a lot of time i'm so used to that plugin that I didn't think about it

@ivorscott
Copy link

@boredwookie Thank you god.

@bluemoehre
Copy link

Hitting same problem on MacOS Sierra, Node 8.1.0, VSCode / Terminal / iTerm - doesn't matter. What is causing this?

@alisianoi
Copy link

alisianoi commented Jun 13, 2017

Will second @bluemoehre here: following the quickstart to the letter, getting a Cannot read property 'on' of undefined. My setup is ArchLinux, node is managed by nvm:

nvm --version
0.33.2
node --version
v8.1.0

Also, there is a global electron package installed by the ArchLinux package manager and an local electron in node_modules that I install with npm install electron --save. Removing either global package or wiping the node_modules folder and reinstalling does not fix the issue. My terminal is termite

@alisianoi
Copy link

Oh, apologies, my mistake. I did not know that in the line const {app, BrowserWindow} = require('electron') you actually cannot rename app and BrowserWindow to other varible names. That solved my issue.

@searene
Copy link

searene commented Jun 20, 2017

I realized that I included main.js(which is the entry point of electron) in index.html. Removing it worked.

@j-ackerman
Copy link

@boredwookie thank you, this solved my issue and likely saved me hours of trial and error

@MggMuggins
Copy link

@boredwookie Do you have any idea what causes this? I'd been using Terminal-Fusion (which is a Linux fork of Platformio Term), and had the same problem. Any idea what the cause is?

@gsilvestri
Copy link

gsilvestri commented Sep 2, 2017

I'm using VS Code to develop Angular JS application and I have exactly the same problem as VS Code is built on top of Atom.

The problem is that you have the same problem using the debugger tool in VS Code... so: no Atom terminal means no debugger.

Using the Ubuntu terminal instead the Atom/VS Code terminal is a workaround, not a solution: needs to makes electron working with Atom terminal.

@dezryth
Copy link

dezryth commented Sep 13, 2017

I'm running into this as well with VS Code

@MggMuggins
Copy link

Do we think it's an atom bug then, rather than the specific terminal?

@wave100
Copy link

wave100 commented Jan 16, 2018

@searene Cheers, that's what I did too. Would've taken me forever to figure it out.

@avoitishin
Copy link

Missing below step will cause this issue:

Turning this Node application into an Electron application is quite simple - we merely replace the node runtime with the electron runtime.

@tbadalov
Copy link

For me using "start": "electron ." script worked. Then just call npm start instead of node index.js

@markobalogh2
Copy link

One implication of this issue appears to be that an electron application cannot be launched from a node script via a child_process - has anyone figured out how to do this?

@MggMuggins
Copy link

At this point it seems like a rather problematic electron bug. Maybe do some digging and see if you can find out what's causing it. Just happens when running via node API calls? Can you call it using the node runtime but using a lower-level interface (is there a way to use raw syscalls in node)?

@Ynoaparast
Copy link

I'm very new to Electron but I figured out the solution to my problem:

I have a very basic Electron application that consists of two files:

  • main.js
  • index.html

I got the error "Uncaught TypeError: Cannot read property 'on' of undefined", in the dev-tools console, despite my application opening and functioning properly.

My mistake:

I had added a <script src = "path/to/main.js"> in my index.html file, because I wanted an onclick-event to call a function in my main.js Removing this <script> tag removed the error.

@hatton
Copy link

hatton commented Jul 11, 2018

With electron 2.0, this is what works in the renderer process:

const { app } = require("electron").remote;

@jdevitt
Copy link

jdevitt commented Sep 3, 2018

The worked for me in VSCode

create .vscode folder in root and create launch.json

{

"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceFolder}\\PATH TO\\YOUR ENTRY POINT.js",
        "stopOnEntry": false,
        "args": [],
        "cwd": "${workspaceFolder}",
        "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd","runtimeArgs": [
            ".",
            "--enable-logging"
        ],
        "env": {
            
        },
        "console": "internalConsole",
        "sourceMaps": false,
    }
]

}

@svmi3195
Copy link

For me this error happened when I, like searene above, occasionally linked main.js in index.html and removing it fixed the problem.

@emmanuel39hanks
Copy link

replace your scripts with:
"scripts": {
"test": "electron .",
"start": "electron ."
}

@martynov-oleg
Copy link

Я понял, что я включил main.js(который является точкой входа электрона) в index.html. Удаление это сработало.

sanks;)

@krishnappahuded
Copy link

In my application it's throwing different error like
TypeError: Cannot set property '_nodeValue' of undefined TypeError: Cannot set property '_nodeValue' of undefined

If we have any solution please help me.

@EduardoSantos7
Copy link

In my case the solution appears after check the python-shell official documentation. 'electron' is correct and be sure to pass run function all the parameters that the function needs (following some tutorials, I was not writting arguments: options) also be sure to write correctly PythonShell

const { app, BrowserWindow } = require('electron')

let { PythonShell } = require('python-shell')

PythonShell.run('hello.py', null, function (err, results) {
    if (err) throw err;
    console.log('hello.py finished.');
    console.log('results', results);
});

Hope be useful!

@jabedhossain
Copy link

One potential cause of this problem can be tied back to executing the electron app from a terminal extension in the Atom editor. This answer on StackOverflow helped me get past the problem: http://stackoverflow.com/a/40664925/1076507. Using a system terminal instead of platformio-ide-terminal plugin inside the Atom editor addressed my problem.

Apparently this is the case why it was showing an exception in VS Code while debugging. It did run in power-shell terminal but showed exception in VS Code debugger, the solution was to add a configuration file for debugging.

Make sure to uncheck, 'All Exceptions' and 'Uncaught Exceptions' from the debugger window. It should debug smoothly.

@ahmedtahabaqi
Copy link

write app in lower case

@levon-isajanyan
Copy link

macOS Catalina problem solved by adding into package json "electron ."
{ "name": "universal-local-dev-tools", "version": "xxx", "description": "xxx ", "main": "xxx", "scripts": { "start": "electron .", "test": "electron ." }, "keywords": [ "xxx", "xxx" ], "author": "xxx", "license": "xxx", "dependencies": {}, "devDependencies": { "electron": "xxx" } }

@codedcontainer
Copy link

That is exactly the problem.
"electron" is not the correct package name!

Yep, @dmr. 👍

If you have a globally installed module called electron (not associated with Electron.js) you'll have a conflict @ibufu. You'll need to remove it and use electron-prebuilt for Electron.js apps.

// cc @chezstov @vertexportus

  1. A package named electrong-prebuild can no longer be found.
  2. Installing the latest version in local node_module folder still returns this error.

@codedcontainer
Copy link

This issue is really old. Removing node_modules and reinstalling will probably fix the issue

Deleting you local node_modules folder and reinstalling does not fix the problem.

@codedcontainer
Copy link

One potential cause of this problem can be tied back to executing the electron app from a terminal extension in the Atom editor. This answer on StackOverflow helped me get past the problem: http://stackoverflow.com/a/40664925/1076507. Using a system terminal instead of platformio-ide-terminal plugin inside the Atom editor addressed my problem.

On Windows tried to use PowerShell and command prompt outside of Visual Studio. Still getting the same error.

@codedcontainer
Copy link

If you use the example project you can see that they are calling electron . in their package.json file. If you have your code written in a different location you can rewrite this to something like the below:

"scripts":{
    "start": "electron ./directory/index.js"
}

I wish they were more transparent about this in their documentation.

@pferreirafabricio
Copy link

For me using "start": "electron ." script worked. Then just call npm start instead of node index.js

Thank you, this worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests